我有一个移动的物体(船)和几个子物体(其炮塔)。无论舰船朝向哪个方向,炮塔都将朝着玩家目标旋转。问题是,除非船垂直向上旋转,否则炮塔会疯狂旋转。
旋转炮塔的代码如下:
//Rotate towards player
dir = PlayerScript.GlobalVariables.playerPosition - myPosition;
angleToTarget = Vector2.Angle(dir, transform.up);
angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90;
transform.localRotation = Quaternion.RotateTowards(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), turnSpeed);
用该代码实例化飞船。更改旋转会更改初始旋转。旋转= 180时,它垂直向上旋转:
newEnemyShip = Object.Instantiate(enemyShip2, new Vector3(mousePosition.x, mousePosition.y, 0), Quaternion.Euler(210, 0, rotation));
该船的初始旋转方向永远不变。它的运动代码是:
//moves in straight line at constant speed
transform.Translate(Vector3.up * currentSpeed * Time.deltaTime, Space.Self);
它还具有将其锁定到2d平面上的功能:
//Lock rotation on 2d plane
Quaternion q = transform.rotation;
q.eulerAngles = new Vector3(0, 0, q.eulerAngles.z);
transform.rotation = q;
任何帮助将不胜感激!
答案 0 :(得分:1)
我知道了!我不确定原始代码是什么问题,但是炮塔的以下代码有效:
//Rotate towards player
dir = PlayerScript.GlobalVariables.playerPosition - myPosition;
angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90;
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), turnSpeed);
Bijan,感谢您抽出宝贵的时间来查找和回复
答案 1 :(得分:1)
为什么不使用animateRemove
?