Third Person Controller Unity 3D - Rigid body Vs Character Controller - Player Character Animation

3 min read 2 hours ago
Published on Oct 25, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the process of implementing a third-person controller in Unity 3D, focusing on the differences between using a Rigidbody and a Character Controller. By the end of this tutorial, you will have a better understanding of player character animation and the best practices for creating an engaging third-person perspective game.

Step 1: Setting Up the Unity Project

  • Open Unity and create a new project or open an existing one.
  • Ensure you have the necessary assets for your game, including a player model and animations.
  • Import the required Unity packages for physics and animation if not already included.

Step 2: Adding the Player Character

  • Drag and drop your player model into the scene.
  • Ensure the player model has a proper scale and positioning.
  • Add a Rigidbody component to your player model:
    • Select the player object.
    • In the Inspector window, click "Add Component."
    • Search for and select "Rigidbody."
  • Adjust Rigidbody properties:
    • Set Mass to an appropriate value (e.g., 1).
    • Enable Use Gravity.
    • Disable Is Kinematic to allow physics interactions.

Step 3: Implementing the Character Controller

  • Alternatively, you can use a Character Controller for smoother movement:
    • Remove the Rigidbody component if added earlier.
    • Add a Character Controller component:
      • Select the player object.
      • In the Inspector window, click "Add Component."
      • Search for and select "Character Controller."
  • Adjust Character Controller properties:
    • Set Center and Radius to fit your player model.
    • Adjust Height based on your character's dimensions.

Step 4: Writing the Player Movement Script

  • Create a new C# script named PlayerMovement.
  • Attach the script to the player object.
  • Open the script and implement basic movement logic:
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 5f;
    private CharacterController controller;

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

    void Update()
    {
        float moveX = Input.GetAxis("Horizontal");
        float moveZ = Input.GetAxis("Vertical");
        
        Vector3 move = transform.right * moveX + transform.forward * moveZ;
        controller.Move(move * speed * Time.deltaTime);
    }
}
  • Save the script and return to Unity.

Step 5: Adding Animation to the Character

  • Import your character animations into Unity.
  • Create an Animator Controller:
    • Right-click in the Project window, select "Create" > "Animator Controller."
    • Assign the Animator Controller to your player model.
  • Set up animations:
    • Open the Animator window and create states for idle, walking, and running.
    • Use transitions based on parameters like speed to switch between states.

Step 6: Testing and Adjusting

  • Enter Play Mode in Unity to test the player movement and animations.
  • Adjust speed and animations based on player feedback.
  • Fine-tune the Rigidbody or Character Controller settings to achieve the desired feel.

Conclusion

In this tutorial, you learned how to set up a third-person controller in Unity 3D using both Rigidbody and Character Controller. You also implemented basic movement and animation for your player character. Next steps could include adding more complex movements, refining animations, and integrating game mechanics like combat or item collection. Continue exploring Unity features to enhance your game development skills!