简体   繁体   English

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

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

相关问题 为什么我的游戏对象移动到错误的位置? - Why is my gameobject moving to the wrong place? 如果我启用/禁用 Unity 游戏对象,为什么我的部分代码没有执行? - Why is part of my code not executed if I enable/disable Unity gameObject? 为什么 BoxCollider 和 GameObject 移动到错误的位置? - Why are BoxCollider and GameObject moving to the wrong place? 为什么我的“角色”仍然在游戏对象中移动而不是游戏 object 破坏 - Why is my “Character” still moving through the gameObject instead of the game object destroying 平稳移动我的Gameobject的最佳方法? - Best way to move my Gameobject smoothly? 为什么在销毁“摄影机”视图中的一个游戏对象时,我的代码每次生成两个游戏对象(而不是一个)? - Why is my code generating two gameObjects(instead of one) at a time when one gameObject out of the Camera view is destroyed? 在移动父游戏对象内移动子游戏对象 - Moving child GameObject inside moving parent GameObject 为什么 GameObject2 向 GameObject1 的原始位置而不是当前位置移动? - Why is GameObject2 moving towards the original position of GameObject1 instead of it's current position? 为什么我的SelectionGrid在Unity Editor中看不到GameObject? - Why my SelectionGrid is not a GameObject visible in Unity Editor? 如何判断GameObject是否在移动? - How to tell if a GameObject is moving or not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM