【问题标题】:unity player moves too fast even with speed 0.000000001f即使速度为 0.000000001f,统一玩家也移动得太快
【发布时间】:2021-04-05 00:45:54
【问题描述】:

我在游戏中的球上附加了以下脚本:

public class MovePlayer : MonoBehaviour {
    //public GameObject packman;
    // Use this for initialization
    private Vector3 currentSpeed;
    void Start () {
        currentSpeed = new Vector3(0.0f, 0.0f, 0.0f);
    }

    // Update is called once per frame
    void Update () {

        if (Input.GetKey(KeyCode.LeftArrow)){
            currentSpeed.x = -(0.0001f);
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            currentSpeed.x = 0.0001f;
        }
        else currentSpeed.x = 0;

        /*if (Input.GetKeyDown(KeyCode.UpArrow))
        {
        }*/

        //move packman
        this.transform.Translate(Time.deltaTime * currentSpeed.x, Time.deltaTime * currentSpeed.y,
            Time.deltaTime * currentSpeed.z);
    }
}

然后我在游戏中触摸左箭头或右箭头,球向一个方向移动非常快,即使我触摸另一个箭头也不会停止。

【问题讨论】:

  • 我不认为我在加速任何事情,不是 +=,而是 =
  • 此外,当您放开钥匙时,它应该设置 speed=0 并停止移动。从给出的代码中我看不出有什么问题。也许清理并重建解决方案。使用调试器确保currentSpeed.xyz的值;由于某种原因,yz 可能不为零。
  • 你展示的脚本没有你描述的效果。您的设置中必须有其他内容会干扰此脚本和/或导致您看到的行为。
  • @Quantic 我发现这是因为我在球上添加了“物理 -> 角色控制器”。删除此组件即可完成工作。为什么角色控制器会产生所描述的效果?
  • 我很抱歉,你是对的。出于某种原因,我将其视为增量。如果是这种情况,我认为不建议将 currentSpeed 变量类级别设置为可取的,它应该是本地的。

标签: c# unity3d


【解决方案1】:

我发现是因为我添加了“物理 -> 角色控制器” 球。删除此组件即可完成工作。为什么是性格 控制器产生所描述的效果? ——丹尼尔·罗卡·洛佩兹

这听起来像是您意外添加的角色控制器,具有对象行为方式的预写值。
因此,在您的 MovePlayer 脚本之上,您还从 CharacterController 获得了运动。

【讨论】:

    【解决方案2】:

    像这样使用 FixedUpdate()

        public class MovePlayer : MonoBehaviour {
        //public GameObject packman;
        // Use this for initialization
        private Vector3 currentSpeed;
        void Start () {
            currentSpeed = new Vector3(0.0f, 0.0f, 0.0f);
        }
    
        // Update is called once every 1/60th second
        void FixedUpdate () {
    
            if (Input.GetKey(KeyCode.LeftArrow)){
                currentSpeed.x = -(0.0001f);
            }
            else if (Input.GetKey(KeyCode.RightArrow))
            {
                currentSpeed.x = 0.0001f;
            }
            else currentSpeed.x = 0;
    
            /*if (Input.GetKeyDown(KeyCode.UpArrow))
            {
            }*/
    
            //move packman
            this.transform.Translate(Time.deltaTime * currentSpeed.x, Time.deltaTime * 
            currentSpeed.y, Time.deltaTime * currentSpeed.z);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 2023-01-02
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多