Unwanted gameObject movement after jumping.

Pretense:

~~I want to stay away from Input.GetAxis as it only caused me issues when developing the movement I wanted.

~~Ballistic jumping is intentional.

I’m trying to imitate the character movement from WoW. I’ve got the feel of the movement and jumping down, but my gameObject was only moving along the world axis. So I’ve tried to localize the character movement and it works well… while I’m on the ground.

If I jump while my gameObject’s rotation is 0, the gameObject will move and jump just fine. However, if there is ANY rotation on the gameObject, my character will jump but will arc away from the axis i was jumping on. I know which piece of code is causing the unwanted movement, but that piece of code is also what allows the gameObject to move freely off of the world axis.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    #region Variables
    // Allows access to CharacterController component //
    private CharacterController controller;

    public float turnSpeed;
    public float moveSpeed;
    public float jumpForce;
    public float gravity;

    private Vector3 moveDir = Vector3.zero;
    #endregion

    private void Start()
    {
        // Sets controller for use of CharacterController component //
        controller = gameObject.GetComponent<CharacterController>();
    }

    void Update()
    {
        // Requires player to be touching the ground //
        if (controller.isGrounded)
        {
            moveDir = new Vector3(0, 0, 0); // Stops movement if no input //

            PlayerMovement(KeyCode.W, Vector3.forward, moveSpeed);
            PlayerMovement(KeyCode.Q, Vector3.left, moveSpeed);
            PlayerMovement(KeyCode.S, Vector3.back, moveSpeed);
            PlayerMovement(KeyCode.E, Vector3.right, moveSpeed);

            PlayerMovement(KeyCode.A, Vector3.down); // Basic turning movement //
            PlayerMovement(KeyCode.D, Vector3.up);   //                        //

            JumpMovement();
        }

        // Applies gravity to the controller //
        moveDir.y -= gravity * Time.deltaTime;

        // Sets movement local to player //
        //                                          //
        //                                          //
        //                                          //
        // This is causing the air movement problem //
        moveDir = transform.TransformDirection(moveDir);
        // Moves player //
        controller.Move(moveDir * Time.deltaTime);
    }

    // Controls player movement (QWSE) //
    void PlayerMovement(KeyCode key, Vector3 vector, float moveSpeed)
    {
        if (Input.GetKey(key))
        {
            moveDir += vector * moveSpeed;
        }
    }

    // Controls player turning (AD) //
    void PlayerMovement(KeyCode key, Vector3 vector)
    {
        if (Input.GetKey(key))
        {
            controller.transform.Rotate(vector * turnSpeed);
        }
    }

    // Controls player jumping mechanic //
    void JumpMovement()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            moveDir.y = jumpForce;
        }
    }

I got it fixed before anyone got to comment, lol.

For anyone caring to know what the fix was, all I did was move moveDir = transform.TransformDirection(moveDir); right after when JumpingMovement(); is called in the update.

I assume what this does is makes the localization only happen while the gameObject is grounded, but after any input has been delivered to change the direction.