Third Person Shooter (Unity Tutorial) Ep 1 Movement and Camera

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

Table of Contents

Introduction

In this tutorial, we will guide you through the process of creating a third-person shooter game in Unity, focusing on player movement and camera setup. This tutorial is part of a series aimed at beginners and intermediate users who want to develop their own game mechanics. By the end of this guide, you'll have a basic understanding of character movement and camera control in a 3D environment.

Step 1: Scene and Player Setup

To begin, you need to set up your Unity scene and player character.

  1. Create a New Project

    • Open Unity and create a new 3D project.
  2. Set Up the Scene

    • In the Hierarchy, right-click and create a new GameObject. Name it "Player".
  3. Add a Character Controller

    • Select the Player object.
    • In the Inspector, click "Add Component" and search for "Character Controller". Add it.
  4. Create a Ground

    • Right-click in the Hierarchy and create a 3D object (e.g., Plane) to serve as the ground.
  5. Adjust the Camera

    • Position the camera to get a good view of the Player object. Set the camera's position to (0, 5, -10) and rotation to (20, 0, 0).

Step 2: Character Movement

Next, we will implement character movement using C# scripting.

  1. Create a New Script

    • In the Project panel, right-click in the Assets folder, go to Create > C# Script, and name it "PlayerMovement".
  2. Open the Script

    • Double-click the script to open it in your code editor.
  3. Implement Movement Logic

    • Use the following code to handle player movement:
    using UnityEngine;
    
    public class PlayerMovement : MonoBehaviour
    {
        public CharacterController controller;
        public float speed = 12f;
    
        void Update()
        {
            float x = Input.GetAxis("Horizontal");
            float z = Input.GetAxis("Vertical");
            Vector3 move = transform.right * x + transform.forward * z;
            controller.Move(move * speed * Time.deltaTime);
        }
    }
    
  4. Attach the Script

    • Drag the PlayerMovement script onto the Player object in the Hierarchy.
    • Assign the Character Controller to the script by dragging the Player object into the relevant field in the Inspector.

Step 3: Testing Movement

Now it’s time to test the movement in your scene.

  1. Enter Play Mode

    • Click the Play button in Unity.
  2. Control the Player

    • Use the WASD keys or arrow keys to move your character around the ground.

Step 4: Ground Check

To enhance the gameplay experience, implement a ground check to prevent the player from jumping or falling indefinitely.

  1. Add Ground Check Logic

    • Update the PlayerMovement script with the following code snippet:
    public LayerMask groundMask;
    public float groundDistance = 0.4f;
    private bool isGrounded;
    
    void Update()
    {
        isGrounded = Physics.CheckSphere(transform.position, groundDistance, groundMask);
    }
    
  2. Define Ground Layer

    • Create a new layer in Unity called "Ground" and assign it to your ground object.
    • In the Inspector for the PlayerMovement script, set the groundMask to the Ground layer.

Step 5: Gravity Setup

Adding gravity will create a more realistic movement experience.

  1. Implement Gravity

    • Update the PlayerMovement script to include gravity:
    public float gravity = -9.81f;
    private Vector3 velocity;
    
    void Update()
    {
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f; // Reset vertical velocity when grounded
        }
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
    

Step 6: Camera Setup

Now we will set up the camera to follow the player.

  1. Create a New Script for Camera Control

    • Right-click in the Assets folder, go to Create > C# Script, and name it "CameraFollow".
  2. Implement Camera Logic

    • Open the CameraFollow script and add the following code:
    using UnityEngine;
    
    public class CameraFollow : MonoBehaviour
    {
        public Transform player;
        public Vector3 offset;
    
        void LateUpdate()
        {
            transform.position = player.position + offset;
        }
    }
    
  3. Attach the CameraFollow Script

    • Drag the CameraFollow script onto the Camera object.
    • In the Inspector, set the player reference to the Player object and adjust the offset to (0, 5, -10).

Step 7: Rotating the Camera and Player

Enhance the interaction by allowing camera rotation based on player movement.

  1. Modify the CameraFollow Script

    • Add rotation functionality to the CameraFollow script to keep the camera aligned with the player.
    void Update()
    {
        float horizontalInput = Input.GetAxis("Mouse X");
        player.Rotate(Vector3.up * horizontalInput);
    }
    
  2. Test Rotation

    • Enter Play mode and move the mouse to rotate the camera and player.

Conclusion

Congratulations! You have successfully set up a basic third-person shooter movement and camera system in Unity. Key takeaways from this tutorial include:

  • Setting up a player character with a Character Controller.
  • Implementing movement and gravity mechanics.
  • Configuring a camera to follow the player and respond to input.

As next steps, consider adding animations or enemy AI to further enhance your game. Stay tuned for the next episode in this series for additional features!