简体   繁体   English

不需要移动时的 Unity 3D 移动

[英]Unity 3D movement when no movement is desired

Very new to mono develop and Unity 3d and seem to be having issues with this code.对单声道开发和 Unity 3d 非常陌生,并且似乎在使用此代码时遇到问题。 First off, the code IS working.首先,代码正在运行。 It does what it's supposed to, however, it also does some funky stuff that it's not supposed to.它做了它应该做的事情,但是,它也做了一些它不应该做的时髦的事情。 You are able to look up, down, left and right as well as walk those directions, the bad part is though for some reason my character likes to nudge the direction of the mouse when standing still.您可以向上、向下、向左和向右查看以及在这些方向上行走,但不好的部分是由于某种原因,我的角色喜欢在静止不动时轻推鼠标的方向。 What am I missing?我错过了什么? More efficient/less buggy way of doing this?这样做更有效/更少错误的方式?

public class PlayerControl : MonoBehaviour {

    public float WALK_SPEED = 1.3f;
    public float RUN_SPEED = 4.0f;
    public float STRAFE_SPEED = 5.0f;
    public float ROTATION_SPEED = 300.0f;
    public float JUMP_FORCE = 250.0f;   
    void Start()
    {
    }
    // Update is called once per frame

    void Update () 
    {
        float movementSpeed = WALK_SPEED;
        float strafeSpeed = STRAFE_SPEED;
        float rotationSpeedx = ROTATION_SPEED;
        float rotationSpeedy = ROTATION_SPEED;

        if (Input.GetKey(KeyCode.LeftShift))
        {
            movementSpeed = RUN_SPEED;
        }

        movementSpeed = Input.GetAxis("Vertical")  * movementSpeed * Time.deltaTime;
        strafeSpeed = Input.GetAxis("Horizontal")  * strafeSpeed * Time.deltaTime;
        rotationSpeedx = Input.GetAxis("Mouse X") * rotationSpeedx * Time.deltaTime;
        rotationSpeedy = Input.GetAxis("Mouse Y") * rotationSpeedy * Time.deltaTime;

        Vector3 rotate = new Vector3 (-rotationSpeedy, rotationSpeedx, 0);

        transform.Translate(Vector3.forward * movementSpeed);
        transform.Translate(Vector3.right * strafeSpeed);
        transform.Rotate(rotate);

        if (Input.GetKeyDown(KeyCode.Space) &&
            transform.position.y < 30) 
        {
            rigidbody.AddForce(Vector3.up * JUMP_FORCE);
        }
    }
}

Try freezing rotation constraints on rigidbody component.尝试在刚体组件上冻结旋转约束。 This may stop the nudging.这可能会停止轻推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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