Unity Basics Course: First-Person View and Rotation Function

Unity is a powerful engine that innovatively changes many aspects of modern game development. In this course, we will explain in detail how to implement a first-person perspective and add a rotation feature within that perspective. This will provide an opportunity to create your first gameplay experience in Unity.

1. Installing Unity and Basic Setup

To use Unity, you must first download and install the Unity Hub from the official website. After installation, create a new project and select the “3D” template. The 3D template is suitable for creating perspective-based games like first-person views.

2. Setting Up the First-Person Character Controller

To implement a first-person game in Unity, you need to set up a character controller. This allows the player to explore the game environment. Proceed to the next steps.

2.1 Adding the Character Controller

Right-click in the Hierarchy window of the project and select “3D Object” > “Capsule” to create a capsule. This will be used as the player. Rename the capsule to ‘Player’.

2.2 Adding a Camera

To create an actual first-person view, you need to add a camera on top of the capsule. Select the Player object, right-click, and choose “Camera” to add the camera. The camera should be placed as a child of the Capsule object.

2.3 Adjusting Camera Position

Adjust the camera’s position to match the player’s head height. Generally, set the Y-axis value to 1.5. Use the Transform component to adjust the camera’s position.

3. Implementing Rotation Functionality

Now let’s add the rotation functionality so the player can adjust the view direction. To do this, you need to write a C# script.

3.1 Creating a New Script

Right-click in the Project window and select “Create” > “C# Script” to create a new script and name it “PlayerController”. Drag this script onto the Player object to add it.

3.2 Writing the Script Content

Open the PlayerController script and write the following code to implement the player’s movement and rotation features:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5.0f;
    public float sensitivity = 2.0f;

    private float rotationY = 0.0f;

    void Update()
    {
        // Rotation
        float mouseX = Input.GetAxis("Mouse X") * sensitivity;
        float mouseY = Input.GetAxis("Mouse Y") * sensitivity;

        rotationY -= mouseY;
        rotationY = Mathf.Clamp(rotationY, -90f, 90f); // Limiting view

        Camera.main.transform.localRotation = Quaternion.Euler(rotationY, 0, 0);
        transform.Rotate(Vector3.up * mouseX);

        // Movement
        float moveX = Input.GetAxis("Horizontal");
        float moveZ = Input.GetAxis("Vertical");
        Vector3 move = transform.right * moveX + transform.forward * moveZ;
        CharacterController controller = GetComponent();
        controller.Move(move * speed * Time.deltaTime);
    }
}

            

3.3 Explanation of the Script

The above code implements the following functionalities:

  • Mouse Input: Calculates rotation based on mouse movement.
  • Vertical and Horizontal Input: Adjusts the player’s position through WASD key input.
  • CharacterController: Uses Unity’s CharacterController for physical collision handling while moving.

4. Physics Settings and Improvements

Now that the basic first-person player controller is complete, let’s address a few improvements and physics settings.

4.1 Adjusting Gravity

You need to add gravity using the CharacterController. Add gravity and jump functionality in the PlayerController script:

private Vector3 velocity;

void Update()
{
    ...
    // Adding gravity
    if (controller.isGrounded)
    {
        velocity.y = -0.5f;
    }
    else
    {
        velocity.y += Physics.gravity.y * Time.deltaTime;
    }
    controller.Move(velocity * Time.deltaTime);
}

            

4.2 Adding Jump Functionality

To implement jumping, you should detect space key input so that the player can jump. Add the following code.

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

            

4.3 Visual Improvements

To create a more natural camera perspective, it’s a good idea to add animations for when the player moves. Consider using Animator and Animation Clip for this purpose.

5. Additions and Optimization

To enhance the player’s sensory experience, consider the following additions.

5.1 Adding Sound Effects

Adding sounds for movement and jumping can increase immersion. Learn how to add sounds using the AudioSource component.

5.2 Using Canvas for Interface and HUD Configuration

Add a UI Canvas to provide essential information to the player. The HUD can display health, mini-maps, scores, etc.

5.3 Optimization

To prevent performance degradation of assets or scripts, analyze and optimize performance using the Profiler. Monitor Unity’s memory usage and CPU usage to adjust to optimal conditions.

Conclusion

Through this course, we have learned about the basic implementation of a first-person perspective and rotation functionality in Unity. Based on this foundational knowledge, try using your creativity to create various games.

Unity is a platform with endless possibilities. Enhance your abilities in Unity through practice and experimentation. We will learn about various features and advanced game development processes in future courses. Happy developing!