THIRD PERSON MOVEMENT in Unity

3 min read 24 days ago
Published on Sep 04, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, we'll learn how to create a third-person movement controller in Unity, complete with a moving camera. This is an essential skill for developing 3D games, as it enhances player experience by providing intuitive character control.

Step 1: Setting Up the Project

  1. Create a New Unity Project

    • Open Unity Hub and start a new project.
    • Choose the 3D template for a better environment setup.
  2. Import Necessary Assets

    • Download the third-person controller asset from the Unity Asset Store.
    • Import the asset into your project by dragging it into the Unity Editor or using the Asset Store window.
  3. Organize Your Scene

    • Set up your scene by adding a plane for the ground.
    • Add a third-person character prefab from the imported assets to your scene.

Step 2: Configuring the Camera

  1. Add a Camera to Your Scene

    • Drag and drop a camera into the scene.
    • Position the camera behind the character to achieve a third-person perspective.
  2. Write a Camera Follow Script

    • Create a new C# script named CameraFollow.
    • Attach the script to the camera.
    • Use the following code to make the camera follow the player smoothly:
    public Transform player;
    public Vector3 offset;
    
    void LateUpdate() {
        transform.position = player.position + offset;
    }
    
  3. Adjust the Camera Offset

    • In the Unity Inspector, set the offset values to place the camera at a suitable distance behind the player (e.g., new Vector3(0, 5, -10)).

Step 3: Implementing Character Movement

  1. Create a Character Controller Script

    • Create a new C# script called PlayerMovement and attach it to the character.
    • Use the following code to handle player movement:
    public float speed = 5.0f;
    
    void Update() {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector3 direction = new Vector3(horizontal, 0, vertical).normalized;
        
        if (direction.magnitude >= 0.1f) {
            Vector3 moveDirection = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0) * direction;
            transform.Translate(moveDirection * speed * Time.deltaTime, Space.World);
        }
    }
    
  2. Adjust Movement Speed

    • In the Inspector, set the speed variable to your desired value (e.g., 5).
  3. Test the Movement

    • Play the scene and check if the character moves according to the camera's orientation.

Step 4: Adding Animation (Optional)

  1. Integrate Animator Component

    • Add an Animator component to your character.
    • Create animation clips for idle, walking, and running states.
  2. Set Up Animation Transitions

    • In the Animator window, create transitions between the various states based on player input.
  3. Update PlayerMovement Script for Animation

    • Include code to trigger animations in the PlayerMovement script:
    Animator animator;
    
    void Start() {
        animator = GetComponent<Animator>();
    }
    
    void Update() {
        // existing movement code
        animator.SetFloat("Speed", direction.magnitude);
    }
    

Conclusion

You've successfully created a third-person movement controller in Unity! You can now expand upon this foundation by adding features like jumping, shooting, or more complex animations. Consider exploring Unity's documentation and tutorials for further learning. Happy game developing!