Reputation: 11
I want object, when spawned, to turn and travel towards my enemy object. I've already got the travel working but the rotation towards the enemy doesn't seem to work.
Here's my current code:
GameObject newRocket = GameObject.FindGameObjectWithTag("Enemy");
direction = (cil.transform.position - novaStrela.transform.position);
rotationDirection = Quaternion.LookRotation(direction);
newRocket = Instantiate (rocket, transform.position, rotationDirection) as GameObject;
newRocket.rigidbody.AddForce ((target.transform.position - transform.position).normalized * projectileSpeed);
I know it's a bit of a mess, but I would be glad if you help me. Thank you so far
Upvotes: 1
Views: 2667
Reputation: 2167
Thry this :
Vector3 v3Dir = transform.position - _destination.position;
float angle = Mathf.Atan2(v3Dir.y, v3Dir.x) * Mathf.Rad2Deg;
transform.eulerAngles = new Vector3(0, 0, angle);
where transform.position is the position of the rocket (thing you want to look at the object) and _destination.position is the position of the object to look at. You might need to fiddle around since this is my code from a 2D game.
Upvotes: 3