即使速度为0.000000001f,统一玩家也会移动太快

时间:2016-04-16 17:13:49

标签: c# unity3d

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

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);
    }
}

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

2 个答案:

答案 0 :(得分:1)

  

我发现这是因为我添加了"物理 - >角色控制器"至   球。删除此组件完成了这项工作。为什么是性格   控制器产生所描述的效果? - Daniel Roca Lopez

这听起来像是你意外添加的角色控制器,有预先写好的Object如何表现的值。
因此,在您的MovePlayer脚本之上,您还可以从CharacterController获得Movement。

答案 1 :(得分:0)

像这样使用 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);
    }
}