简体   繁体   English

将对象旋转到另一个对象-C#Unity

[英]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. 其中transform.position是火箭的位置(要查看对象的位置),_destination.position是要查看的对象的位置。 You might need to fiddle around since this is my code from a 2D game. 您可能需要摆弄一下,因为这是我从2D游戏获得的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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