3rd Person Character Movement

Please help, I have no idea what I am doing and am new to Unity. I’ve looked through similar questions, but haven’t found anything that helps me.

What I want, is to create a playable 3rd person character where the camera looks over the player’s shoulder. The camera bit is easy enough, I just made the camera a child of the player character, so it points in whichever direction the character is facing, which is perfect.

Where I’m having trouble is with moving the character and rotating (turning) the character at the same time.

The character moves around the map using wasd, as well as combinations of that for moving diagonally. The character turns (or looks around) using the map. The problem is, I can’t do both at the same time. As soon as I push one of the movement keys (wasd) the character immediately starts walking in that direction, but becomes unresponsive to mouse movements. For example, if I wanted to walk my character down a hallway and turn left through a door at the end of it I would want to just hold the W key to move forward, and once at the end of the hallway, then move the mouse to the left making the character walk through the door.

Instead what I have to do using my current script is walk the character down the hallway using W. Then stop moving, move the mouse to the left to turn the character, and then I can push W again.

How can I fix this?

Below is the script I’m trying to use:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 6f;            // The speed that the player will move at.
    
    Vector3 movement;                   // The vector to store the direction of the player's movement.
    Animator anim;                      // Reference to the animator component.
    Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
    int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
    float camRayLength = 100f;          // The length of the ray from the camera into the scene.
    
    void Awake ()
    {
        // Create a layer mask for the floor layer.
        floorMask = LayerMask.GetMask ("Floor");
        
        // Set up references.
        anim = GetComponent<Animator>();
        playerRigidbody = GetComponent<Rigidbody>();
    }
    
    
    void FixedUpdate ()
    {
        // Store the input axes.
        float h = Input.GetAxisRaw ("Horizontal");
        float v = Input.GetAxisRaw ("Vertical");
        
        // Move the player around the scene.
        Move ();
        
        // Turn the player to face the mouse cursor.
        Turning ();
        
        // Animate the player.
        Animating (h, v);
    }
    
    void Move ()
    {
        if(Input.GetKey(KeyCode.W)) {
            if(Input.GetKey(KeyCode.D)){
                transform.position += (transform.forward + transform.right) * Time.deltaTime * speed;
            } else if(Input.GetKey(KeyCode.A)){
                transform.position += (transform.forward - transform.right) * Time.deltaTime * speed;
            } else{
                transform.position += transform.forward * Time.deltaTime * speed;
            }
        }
        else if(Input.GetKey(KeyCode.S)) {
            if(Input.GetKey(KeyCode.A)){
                transform.position -= (transform.forward + transform.right) * Time.deltaTime * speed;
            } else if(Input.GetKey(KeyCode.D)){
                transform.position -= (transform.forward - transform.right) * Time.deltaTime * speed;
            } else{
                transform.position -= transform.forward * Time.deltaTime * speed;
            }
        }
        else if(Input.GetKey(KeyCode.A)) {
            if(Input.GetKey(KeyCode.W)){
                transform.position += (transform.forward - transform.right) * Time.deltaTime * speed;
            } else if(Input.GetKey(KeyCode.S)){
                transform.position -= (transform.forward - transform.right) * Time.deltaTime * speed;
            } else{
                transform.position -= transform.right * Time.deltaTime * speed;
            }
        }
        else if(Input.GetKey(KeyCode.D)) {
            if(Input.GetKey(KeyCode.W)){
                transform.position += (transform.forward + transform.right) * Time.deltaTime * speed;
            } else if(Input.GetKey(KeyCode.S)){
                transform.position -= (transform.forward + transform.right) * Time.deltaTime * speed;
            } else{
                transform.position += transform.right * Time.deltaTime * speed;
            }
        }
        if(Input.GetKey(KeyCode.E)) {
            transform.Rotate(0, Time.deltaTime * speed, 0);
        }
        else if(Input.GetKey(KeyCode.Q)) {
            transform.Rotate(0, Time.deltaTime * (-1)*speed, 0);
        }
        
        playerRigidbody.MovePosition(transform.position);
    }
    
    void Turning ()
    {
     Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(transform.position);
     Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
     
     float angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);
     
     playerRigidbody.MoveRotation(Quaternion.Euler(new Vector3(0f, 180 - angle, 0f)));
    }
    
    float AngleBetweenTwoPoints(Vector3 a, Vector3 b){
        return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
    }
    
    void Animating (float h, float v)
    {
        // Create a boolean that is true if either of the input axes is non-zero.
        bool walking = h != 0f || v != 0f;
        
        // Tell the animator whether or not the player is walking.
        anim.SetBool ("IsWalking", walking);
    }
}

Still having trouble with this. I’ve tried separating the turning and walking into two different scripts, but pushing buttons on the keyboards makes the mouse stop working.

Here are the two scripts now:

using UnityEngine;

public class PlayerTurning : MonoBehaviour {
    Rigidbody playerRigidbody;          // Reference to the player's rigidbody.

    void Awake ()
    {
        playerRigidbody = GetComponent<Rigidbody>();
    }
	// Update is called once per frame
    void Update ()
    {
        Turning();
    }
    void Turning ()
    {
        Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(transform.position);
        Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
        
        float angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);
        
        playerRigidbody.MoveRotation(Quaternion.Euler(new Vector3(0f, 180 - angle, 0f)));
    }
    
    float AngleBetweenTwoPoints(Vector3 a, Vector3 b){
        return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
    }
}

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 6f;            // The speed that the player will move at.
    
    Vector3 movement;                   // The vector to store the direction of the player's movement.
    Animator anim;                      // Reference to the animator component.
    Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
    int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
    float camRayLength = 100f;          // The length of the ray from the camera into the scene.
    
    void Awake ()
    {
        // Create a layer mask for the floor layer.
        floorMask = LayerMask.GetMask ("Floor");
        
        // Set up references.
        anim = GetComponent<Animator>();
        playerRigidbody = GetComponent<Rigidbody>();
    }
    
    
    void FixedUpdate ()
    {
        // Store the input axes.
        float h = Input.GetAxisRaw ("Horizontal");
        float v = Input.GetAxisRaw ("Vertical");
        
        // Move the player around the scene.
        //Turning();
        Move ();
        
        // Turn the player to face the mouse cursor.
        //Turning ();
        
        // Animate the player.
        Animating (h, v);
    }
    
    void Move ()
    {
        if(Input.GetKey(KeyCode.W)) {
            if(Input.GetKey(KeyCode.D)){
                transform.position += (transform.forward + transform.right) * Time.deltaTime * speed;
            } else if(Input.GetKey(KeyCode.A)){
                transform.position += (transform.forward - transform.right) * Time.deltaTime * speed;
            } else{
                transform.position += transform.forward * Time.deltaTime * speed;
            }
        }
        else if(Input.GetKey(KeyCode.S)) {
            if(Input.GetKey(KeyCode.A)){
                transform.position -= (transform.forward + transform.right) * Time.deltaTime * speed;
            } else if(Input.GetKey(KeyCode.D)){
                transform.position -= (transform.forward - transform.right) * Time.deltaTime * speed;
            } else{
                transform.position -= transform.forward * Time.deltaTime * speed;
            }
        }
        else if(Input.GetKey(KeyCode.A)) {
            if(Input.GetKey(KeyCode.W)){
                transform.position += (transform.forward - transform.right) * Time.deltaTime * speed;
            } else if(Input.GetKey(KeyCode.S)){
                transform.position -= (transform.forward - transform.right) * Time.deltaTime * speed;
            } else{
                transform.position -= transform.right * Time.deltaTime * speed;
            }
        }
        else if(Input.GetKey(KeyCode.D)) {
            if(Input.GetKey(KeyCode.W)){
                transform.position += (transform.forward + transform.right) * Time.deltaTime * speed;
            } else if(Input.GetKey(KeyCode.S)){
                transform.position -= (transform.forward + transform.right) * Time.deltaTime * speed;
            } else{
                transform.position += transform.right * Time.deltaTime * speed;
            }
        }
        if(Input.GetKey(KeyCode.E)) {
            transform.Rotate(0, Time.deltaTime * (25)*speed, 0);
            if(Input.GetKey(KeyCode.W)) {
                if(Input.GetKey(KeyCode.D)){
                    transform.position += (transform.forward + transform.right) * Time.deltaTime * speed;
                } else if(Input.GetKey(KeyCode.A)){
                    transform.position += (transform.forward - transform.right) * Time.deltaTime * speed;
                } else{
                    transform.position += transform.forward * Time.deltaTime * speed;
                }
            }
            else if(Input.GetKey(KeyCode.S)) {
                if(Input.GetKey(KeyCode.A)){
                    transform.position -= (transform.forward + transform.right) * Time.deltaTime * speed;
                } else if(Input.GetKey(KeyCode.D)){
                    transform.position -= (transform.forward - transform.right) * Time.deltaTime * speed;
                } else{
                    transform.position -= transform.forward * Time.deltaTime * speed;
                }
            }
            else if(Input.GetKey(KeyCode.A)) {
                if(Input.GetKey(KeyCode.W)){
                    transform.position += (transform.forward - transform.right) * Time.deltaTime * speed;
                } else if(Input.GetKey(KeyCode.S)){
                    transform.position -= (transform.forward - transform.right) * Time.deltaTime * speed;
                } else{
                    transform.position -= transform.right * Time.deltaTime * speed;
                }
            }
            else if(Input.GetKey(KeyCode.D)) {
                if(Input.GetKey(KeyCode.W)){
                    transform.position += (transform.forward + transform.right) * Time.deltaTime * speed;
                } else if(Input.GetKey(KeyCode.S)){
                    transform.position -= (transform.forward + transform.right) * Time.deltaTime * speed;
                } else{
                    transform.position += transform.right * Time.deltaTime * speed;
                }
            }
        }
        else if(Input.GetKey(KeyCode.Q)) {
            transform.Rotate(0, Time.deltaTime * (-25)*speed, 0);
            if(Input.GetKey(KeyCode.W)) {
                if(Input.GetKey(KeyCode.D)){
                    transform.position += (transform.forward + transform.right) * Time.deltaTime * speed;
                } else if(Input.GetKey(KeyCode.A)){
                    transform.position += (transform.forward - transform.right) * Time.deltaTime * speed;
                } else{
                    transform.position += transform.forward * Time.deltaTime * speed;
                }
            }
            else if(Input.GetKey(KeyCode.S)) {
                if(Input.GetKey(KeyCode.A)){
                    transform.position -= (transform.forward + transform.right) * Time.deltaTime * speed;
                } else if(Input.GetKey(KeyCode.D)){
                    transform.position -= (transform.forward - transform.right) * Time.deltaTime * speed;
                } else{
                    transform.position -= transform.forward * Time.deltaTime * speed;
                }
            }
            else if(Input.GetKey(KeyCode.A)) {
                if(Input.GetKey(KeyCode.W)){
                    transform.position += (transform.forward - transform.right) * Time.deltaTime * speed;
                } else if(Input.GetKey(KeyCode.S)){
                    transform.position -= (transform.forward - transform.right) * Time.deltaTime * speed;
                } else{
                    transform.position -= transform.right * Time.deltaTime * speed;
                }
            }
            else if(Input.GetKey(KeyCode.D)) {
                if(Input.GetKey(KeyCode.W)){
                    transform.position += (transform.forward + transform.right) * Time.deltaTime * speed;
                } else if(Input.GetKey(KeyCode.S)){
                    transform.position -= (transform.forward + transform.right) * Time.deltaTime * speed;
                } else{
                    transform.position += transform.right * Time.deltaTime * speed;
                }
            }
        }
        
        playerRigidbody.MovePosition(transform.position);
    }
    
    void Animating (float h, float v)
    {
        // Create a boolean that is true if either of the input axes is non-zero.
        bool walking = h != 0f || v != 0f;
        
        // Tell the animator whether or not the player is walking.
        anim.SetBool ("IsWalking", walking);
    }
}