Unity TPS Player Controller - Unity Player Animation 3d Script Third Person Shooter Game Course 2022

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

Table of Contents

Introduction

In this tutorial, we will develop a third-person shooter (TPS) player controller in Unity. This guide is designed for anyone looking to enhance their skills in 3D game development using the Unity game engine. By the end of this tutorial, you will have a functional player controller that can be used in your own TPS game.

Step 1: Setting Up the Unity Project

  • Open Unity and create a new project.
  • Choose the 3D template for your project.
  • Organize your project folder by creating subfolders like Scripts, Prefabs, Animations, and Models.

Step 2: Importing Necessary Assets

  • Download or create 3D models for your player character and environment.
  • Import these models into your Unity project by dragging them into the Assets folder.
  • Ensure that the models have the correct materials applied for visual consistency.

Step 3: Creating the Player GameObject

  • In the Hierarchy window, right-click and create a new GameObject.
  • Name it "Player".
  • Attach the 3D model of your character to the Player GameObject by dragging it onto the Player in the Hierarchy.

Step 4: Adding the Character Controller

  • With the Player GameObject selected, click on Add Component in the Inspector.
  • Search for and add the Character Controller component.
  • Adjust the height, radius, and center settings of the Character Controller to fit your character model.

Step 5: Scripting the Player Movement

  • In the Scripts folder, create a new C# script named PlayerController.
  • Double-click to open the script in your preferred code editor.
  • Implement the following basic movement code:
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public CharacterController controller;
    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    private Vector3 velocity;
    private bool isGrounded;

    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * speed * Time.deltaTime);

        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
}
  • Save the script and return to Unity.

Step 6: Attaching the Script

  • Select the Player GameObject in the Hierarchy.
  • In the Inspector, add the PlayerController script component.
  • Drag the Character Controller component into the corresponding field in the script.

Step 7: Setting Up Camera Follow

  • Create a new empty GameObject named "CameraFollow".
  • Set its position to be above and behind the Player.
  • In the Camera, attach the following script to make it follow the player:
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Transform player;
    public Vector3 offset;

    void LateUpdate()
    {
        transform.position = player.position + offset;
    }
}
  • Assign the Player GameObject to the player field in the CameraFollow script.

Step 8: Testing the Player Controller

  • Hit the Play button in Unity.
  • Use the W, A, S, D keys to move the player character and the Spacebar to jump.
  • Ensure that the player moves smoothly and interacts properly with the environment.

Conclusion

You have successfully created a basic TPS player controller in Unity. This setup allows for character movement and jumping mechanics, serving as a foundation for further enhancements like shooting mechanics, animations, and AI. Consider exploring these features as you continue developing your game. Happy gaming!