简体   繁体   中英

Rotate object towards another object - C# Unity

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM