This question already has an answer here:

I'm an newbie in game developing. So i have a script for a camera that i attach to the player. I was wondering how to make a Third Person Controller with WASD controls that moves in the direction that the camera is facing. If you need it here's the camera script that i use

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThirdPersonCamera : MonoBehaviour {
    //define some constants
    private const float LOW_LIMIT = 0.0f;
    private const float HIGH_LIMIT = 85.0f;

    //these will be available in the editor
    public GameObject theCamera;
    public float followDistance = 5.0f;
    public float mouseSensitivityX = 4.0f;
    public float mouseSensitivityY = 2.0f;
    public float heightOffset = 0.5f;

    //private variables are hidden in editor
    private bool isPaused = false;

    // Use this for initialization
    void Start () {
        //place the camera and set the forward vector to match player
        theCamera.transform.forward = gameObject.transform.forward;
        //hide the cursor and lock the cursor to center
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;

    // Update is called once per frame
    void Update () {
        //if escape key (default) is pressed, pause the game (feel free to change this)
        if (Input.GetButton("Cancel"))
            //flip the isPaused state, hide/unhide the cursor, flip the lock state
            isPaused = !isPaused;
            Cursor.visible = !Cursor.visible;
            Cursor.lockState = Cursor.lockState == CursorLockMode.Locked ?
            CursorLockMode.None : CursorLockMode.Locked;

            //if we are not paused, get the mouse movement and adjust the camera
            //position and rotation to reflect this movement around player
            Vector2 cameraMovement = new Vector2(Input.GetAxis("Mouse X"),Input.GetAxis("Mouse Y"));

            //first we place the camera at the position of the player + height offset
            theCamera.transform.position = gameObject.transform.position + new Vector3(0,heightOffset,0);

            //next we adjust the rotation based on the captured mouse movement
            //we clamp the pitch (X angle) of the camera to avoid flipping
            //we also adjust the values to account for mouse sensitivity settings
            theCamera.transform.eulerAngles = new Vector3(
                Mathf.Clamp(theCamera.transform.eulerAngles.x + cameraMovement.y * mouseSensitivityY, LOW_LIMIT, HIGH_LIMIT),
                theCamera.transform.eulerAngles.y + cameraMovement.x * mouseSensitivityX, 0);

            //then we move out to the desired follow distance
            theCamera.transform.position -= theCamera.transform.forward * followDistance;

marked as duplicate by DMGregory unity Jun 29 at 13:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ What did you try? How do the results differ from what you want? \$\endgroup\$ – DMGregory May 30 at 8:50

Not sure I understand what you really need but I want to suggest two things:

  1. Usually you should not move the camera with WASD (keyboard) in a Third Person Controller. You should move your player rotation based on the Mouse X axis during movement.

Example: if player is NOT moving your mouse should be able to move your camera (without moving the player) on Mouse X and Y axis (just remember to change player rotation based on your camera when you start moving). If player is moving your mouse should be able to move on Y axis but while moving on the X axis it should also rotate your player. So, if your player is moving forward with W if you use your mouse you should be able to rotate, if you only press D your player should be able to move right (but your camera should face forward).

  1. If you really want to move your camera based on the WASD (like you should do with a joystick). You just need to rotate the camera by using the player rotation (the contrary of what you do in the first example), just remember that the camera rotate as a circle with the player as center.

For the script part: there are many ways to create a Third Person Controller (also lot's of already made scripts, Unity standard assets got one example that you can use to learn how to make your own), you should try to create a basic script and ask for more specific help when you tried to make it and it's not working (and debug until you find where is not working).

Still, just remember my advice. The key is to move the camera and rotate the player rotation based on the X Mouse axis or the camera rotation based on the player rotation as a center of a circle (always apply only when player is moving). Usually the first is great for mouse/keyboard movement, the last is great with joystick movement.


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