[英]Why this code moving my gameobject in such an odd way?
My Code我的代码
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);
In the update function intended to look at and move to Sensitive, though it points correctly doesn't move correctly and I can figure out why.在更新 function 中,旨在查看并移动到敏感,尽管它正确指向并没有正确移动,我可以找出原因。
transform.Translate
by default interprets the input as a local direction & distance. transform.Translate
默认将输入解释为局部方向和距离。 You're providing the input in world direction & distance, so you should use the optional 2nd parameter and specify Space.World
:您正在提供世界方向和距离的输入,因此您应该使用可选的第二个参数并指定
Space.World
:
transform.Translate(new Vector3(moveDirection.x, moveDirection.y)
* (speed * Time.deltaTime), Space.World);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.