FPS Full Game Tutorial | Unity | Part 1 - Basic Movement

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

Table of Contents

Introduction

In this tutorial, we will explore the basics of creating a first-person shooter (FPS) game using Unity. This guide focuses on implementing basic movement mechanics, which are crucial for player navigation and interaction within the game environment. By the end of this tutorial, you will have a foundational understanding of FPS movement in Unity.

Step 1: Setting Up Your Project

  1. Open Unity and create a new 3D project.
  2. Name your project (e.g., "FPSGame") and select an appropriate location on your computer.
  3. Once the project is created, familiarize yourself with the Unity interface.

Step 2: Creating the Player Object

  1. In the Hierarchy panel, right-click and select 3D Object > Capsule. This will serve as your player character.
  2. Rename the capsule to "Player".
  3. Reset the capsule's transform to ensure it is positioned at the origin (0, 0, 0).
  4. Adjust the capsule’s scale if needed, typically to (1, 2, 1) for a more realistic player height.

Step 3: Adding a Player Controller

  1. With the Player object selected, go to the Inspector panel.
  2. Click Add Component and search for "Rigidbody". Add it to the Player object.
  3. Ensure that the Use Gravity option is checked.
  4. Adjust the Rigidbody settings:
    • Set Mass to 1.
    • Set Drag to 0.
    • Set Angular Drag to 0.05.

Step 4: Creating the Player Movement Script

  1. In the Project panel, create a new folder named "Scripts".
  2. Right-click in the Scripts folder, select Create > C# Script, and name it "PlayerMovement".
  3. Open the script in your code editor and implement the following code:
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 5f;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        rb.AddForce(movement * moveSpeed);
    }
}
  1. Save the script and return to Unity.

Step 5: Attaching the Movement Script

  1. Select the Player object in the Hierarchy.
  2. In the Inspector panel, click Add Component and search for your "PlayerMovement" script to attach it.
  3. Adjust the Move Speed variable in the Inspector if necessary (default is 5).

Step 6: Configuring the Camera

  1. Create a new camera by right-clicking in the Hierarchy and selecting Camera.
  2. Position the camera as a child of the Player object by dragging it onto the Player in the Hierarchy.
  3. Adjust the camera's transform values to align it properly above the Player (e.g., position it at (0, 1.5, 0)).
  4. Set the camera’s field of view to enhance the FPS experience.

Step 7: Testing the Movement

  1. Click the Play button in Unity to enter Play mode.
  2. Use the W, A, S, and D keys to move the Player object around the scene.
  3. Observe the movement responsiveness and adjust the moveSpeed value in the PlayerMovement script if necessary.

Conclusion

Congratulations! You have successfully implemented basic movement for a first-person shooter game in Unity. Key takeaways from this tutorial include setting up player objects, adding components for movement, and creating scripts to control player interactions.

Next steps could involve enhancing the player experience by adding shooting mechanics, animations, or improving the environment. Keep practicing and exploring other features of Unity as you continue to develop your FPS game.