Modelling 3D Movement Physics

Started by
0 comments, last by uselessChiP 9 years ago

Hi, i'm trying to model a movement system for a fps like game, i want something like the movement in borderlands 2. I don't know very much about physics and i'm having a hard time finding something useful online.

This is what i got so far (i'm using unity and the player uses a character controller):


    void Update()
    {
        MouseRotation();
        Movement();
    }

    private void Movement()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        bool isMoving = (h != 0 || v != 0) ? true : false;

        if (controller.isGrounded)
        {
            if (isMoving)
                speed += acceleration ;

            speed -= friction;
            if (speed < 0)
                speed = 0;

            if (Input.GetButtonDown("Jump"))
                moveDirection.y = jumpSpeed;
        }
        else
        {
            if (isMoving)
                speed += airAcceleration;

            moveDirection.y -= gravity * Time.deltaTime;
        }

        if (speed > maxSpeed)
            speed = maxSpeed;

        moveDirection = new Vector3(h * speed, moveDirection.y, v * speed);
       // moveDirection.y -= gravity * Time.deltaTime;
        localMoveDir = transform.TransformDirection(moveDirection);
        controller.Move(localMoveDir * Time.deltaTime);
    }

    private void MouseRotation()
    {
        rotX += Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime;
        rotY += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
        rotY = Mathf.Clamp(rotY, -maxMouseVerticalRotation, maxMouseVerticalRotation);
        camera.rotation = Quaternion.Euler(-rotY, rotX, 0);
        transform.rotation = Quaternion.Euler(0, rotX, 0);
    }

(this code does not work properly cause when i don't press anything the speed goes instantly to 0)

The two main thing that i thing are missing are that, when i press, for example, forward and then back, the character changes direction abruptly and, the most important thing, I think that the speed in borderlands is affected by the mouse rotation, when for example i rotate 180 degree the character slows down and then start again gaining speed.

I'm not sure how to do this (especially the mouse rotation part).

Can you give me some suggestion on how i could model a movement system almost identical to the one borderlands use?

Thanks.

Advertisement
I think i've improved the code, now looks like this:

 void Update()
    {
        MouseRotation();
        Movement();
    }

    private void Movement()
    {
        Vector3 moveDir = Input.GetAxisRaw("Horizontal") * transform.right + Input.GetAxisRaw("Vertical") * transform.forward;
        velocity += (moveDir * acceleration);

        velocity.x = Mathf.Clamp(velocity.x, -maxSpeed, maxSpeed);
        velocity.z = Mathf.Clamp(velocity.z, -maxSpeed, maxSpeed);

        if (Mathf.Abs(velocity.x) - friction > 0)
            velocity.x -= Mathf.Sign(velocity.x) * friction;
        else
            velocity.x = 0;

        if (Mathf.Abs(velocity.z) - friction > 0)
            velocity.z -= Mathf.Sign(velocity.z) * friction;
        else
            velocity.z = 0;

        controller.Move(velocity * Time.deltaTime);
    }

    private void MouseRotation()
    {
        rotX += Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime;
        rotX += Input.GetAxisRaw("Arrow H") * mouseSensitivityX * Time.deltaTime;
        rotX = rotX % 360;
        rotY += Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
        rotY += Input.GetAxis("Arrow V") * mouseSensitivityY * Time.deltaTime;
        rotY = Mathf.Clamp(rotY, -maxMouseVerticalRotation, maxMouseVerticalRotation);
        transform.rotation = Quaternion.Euler(0, rotX, 0);
        camera.rotation = Quaternion.Euler(-rotY, rotX, 0);
    }
Now the problem is that the character moves only on the 4 main axis (forward, backwards, left, right) and at 45 degrees between them.
For example in this image:
uAH55.jpg
The character moves forwards (red arrow) even if it is facing the green arrow.
Do you know what's wrong with the code?

This topic is closed to new replies.

Advertisement