[英]Why this code moving my gameobject in such an odd way?
我的代码
Vector2 moveDirection;
moveDirection = (Sensitive.transform.position - gameObject.transform.position).normalized;
float deg = Mathf.Rad2Deg * Mathf.Atan(moveDirection.x / -moveDirection.y);
if (moveDirection.y < 0) deg -= 180;
Vector3 to = new Vector3(0, 0, deg);
transform.eulerAngles = to;
transform.Translate(new Vector3(moveDirection.x, moveDirection.y) * speed * Time.deltaTime);
在更新 function 中,旨在查看并移动到敏感,尽管它正确指向并没有正确移动,我可以找出原因。
transform.Translate
默认将输入解释为局部方向和距离。 您正在提供世界方向和距离的输入,因此您应该使用可选的第二个参数并指定Space.World
:
transform.Translate(new Vector3(moveDirection.x, moveDirection.y)
* (speed * Time.deltaTime), Space.World);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.