繁体   English   中英

为什么这段代码以如此奇怪的方式移动我的游戏对象?

[英]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.

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