1
\$\begingroup\$

So, I have a simple WASD script, and I've added a part so when Mouse X is moved, the playerBody (character) rotates in that direction, so, when W is pressed (to go forward) the player does not move in the direction turned, but it moves in the same direction. Although the playerBody has turned, the player still moves in the same direction.... why?

using UnityEngine;
using System.Collections;

// This script moves the character controller forward
// and sideways based on the arrow keys.
// It also jumps when pressing space.
// Make sure to attach a character controller to the same game object.
// It is recommended that you make only one call to Move or SimpleMove per frame.

public class Move : MonoBehaviour
{
    CharacterController characterController;

    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public Transform charBody;
    private Vector3 moveDirection = Vector3.zero;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        if (characterController.isGrounded)
        {
            // We are grounded, so recalculate
            // move direction directly from axes

            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
            moveDirection *= speed;

            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }

        // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
        // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
        // as an acceleration (ms^-2)
        moveDirection.y -= gravity * Time.deltaTime;

        // Move the controller
        characterController.Move(moveDirection * Time.deltaTime);

        float MOUSEY = Input.GetAxis("Mouse Y") * speed * Time.deltaTime;
        float MOUSEX = Input.GetAxis("Mouse X") * speed * Time.deltaTime;

        charBody.Rotate(Vector3.up * MOUSEX);

        


    }
}

Thanks

\$\endgroup\$
1
\$\begingroup\$

Your moveDirection vector is not taking into account the character's orientation because you use world space vectors in the moveDirection calculations.
The new Vector3(MouseX, 0, MouseY) will always point to the position at MouseX units along the world X axis and MouseY units along the world Z axis (i.e., relative to the world center point). And the character will move along this world space vector no matter where the character is looking at.
To have the direction that depends on the character's orientation you need to use the character's directional vectors instead of the world space vectors.
So, to move the character x units forward you need to use the character's transform.forward vector that represents a directional vector that points to the character's 'look' direction.
This vector have magnitude 1 (i.e. the length is 1 unit), so you need to multiply it by your desired x value to get a vector that represents a motion from character's current position forward for the x units.
Same thing for right/left movement, but here you use transform.right vector.

moveDirection = transform.forward * Input.GetAxis("Horizontal") + transform.right * Input.GetAxis("Vertical");
moveDirection *= speed;
\$\endgroup\$
1
  • \$\begingroup\$ Thank you very much :)! It worked :) \$\endgroup\$ – Artificial Engineer May 21 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.