Unity bouncing when colliding with object

2019-08-17 06:05发布

I have made a script with movement, that finally works as i want it to, except one thing... I want it to be a first person game, but the way the movement works now, is on the global axis, which means that W is always torwards one specific direction, no matter what direction my camera is turning... How do i fix this? i want the movement to stay how it is, but with the W key to always be forward depending on the way the camera or player is looking.

Please let me know how my script would look edited, or atleast what part i have to change.

I would also like to add that i would love to be able to do a wall jump, but i am not sure how to add that behavior.

Here is my movement script:

public class MovementScript : MonoBehaviour {

    public float speed;
    public float jumpforce;
    public float gravity = 25;

    private Vector3 moveVector;
    private Vector3 lastMove;
    private float verticalVelocity;
    private CharacterController controller;

    // Use this for initialization
    void Start () {
        controller = GetComponent<CharacterController> ();

        //Låser og gemmer musen
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update () {
        //Låser musen op
        if (Input.GetKeyDown ("escape"))
            Cursor.lockState = CursorLockMode.None;

        moveVector = Vector3.zero;
        moveVector.x = Input.GetAxis("Horizontal");
        moveVector.z = Input.GetAxis("Vertical");

        if (controller.isGrounded) {
            verticalVelocity = -1;

            if (Input.GetButton("Jump")) {
                verticalVelocity = jumpforce;
            }

        } else {
            verticalVelocity -= gravity * Time.deltaTime;
            moveVector = lastMove;
        }

        moveVector.y = 0;
        moveVector.Normalize ();
        moveVector *= speed;
        moveVector.y = verticalVelocity;

        controller.Move (moveVector * Time.deltaTime);
        lastMove = moveVector;
    }
}

2条回答
Explosion°爆炸
2楼-- · 2019-08-17 07:00

You need to go from local to world space:

for example when you want to move on the x-axis regarding the player's orientation, the local vector is (1, 0, 0), which you get from your input axis, but the CharacterController need a world based direction (see the doc of the Move function)

A more complex move function taking absolute movement deltas.

To get this, use Transform.TransformDirection like this

worldMove = transform.TransformDirection(moveVector);
controller.Move (worldMove * Time.deltaTime);

EDIT regarding your issue with the controller moving a bit after releasing the input:

That's because GetAxis gives you a smoothed value. Replace GetAxis by GetAxisRaw and it should work

查看更多
再贱就再见
3楼-- · 2019-08-17 07:03

Modify the final moving code to

controller.Move(moveVector.z * transform.forward * Time.deltaTime);
controller.Move(moveVector.x * transform.right * Time.deltaTime);
controller.Move(moveVector.y * transform.up * Time.deltaTime);

Alternatively, suppose your character is only rotated about the Y axis, you can look into rotating your moveVector by your character's Y rotation so moveVector's forward points parallel to your chracter's forward.

I found a good explaination of rotating a vector here: https://answers.unity.com/questions/46770/rotate-a-vector3-direction.html

Rotating a vector:

moveVector = Quaternion.Euler(0, transform.eulerAngles.y, 0) * moveVector;
查看更多
登录 后发表回答