Unity is a powerful engine for game development that provides the ability to create a variety of 2D and 3D applications.
In this course, we will cover how to rotate the camera’s view up and down in Unity.
Through this process, you will learn how to manipulate the player’s viewpoint and, further, provide an immersive experience within the game.
1. Basic Setup
First, launch Unity and create a new project.
Select the 3D project and set an appropriate project name. After the project is launched, configure the basic scene.
In the scene view, select the Main Camera object and adjust the camera’s position and rotation angles.
The camera serves as the reference to display all game objects, so it must be properly set.
2. Setting Up the Camera and Player Character
To set up the player character, select 3D Object > Cylinder from the GameObject menu to create a basic character model.
This object will become a simple form of the player character. Rename the created cylinder to ‘Player’, and
set the Main Camera as the child of the player.
This will allow the camera to move with the player.
3. Setting Up the Input System
To implement the up and down rotation of the camera, we will write a script for input processing. Create a folder named Scripts inside the Assets folder, and
create a script named CameraController.cs within it.
Paste the following code into the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform player; // Player character
public float sensitivity = 5.0f; // Mouse sensitivity
public float clampAngle = 80.0f; // Rotation limit angle
private float rotX = 0.0f; // Up and down rotation angle
private float rotY = 0.0f; // Left and right rotation angle
void Start()
{
rotX = transform.localEulerAngles.x; // Set initial up and down rotation angle
rotY = transform.localEulerAngles.y; // Set initial left and right rotation angle
Cursor.lockState = CursorLockMode.Locked; // Hide the mouse cursor
}
void Update()
{
rotX -= Input.GetAxis("Mouse Y") * sensitivity; // Calculate up and down rotation with mouse Y input
rotY += Input.GetAxis("Mouse X") * sensitivity; // Calculate left and right rotation with mouse X input
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle); // Limit the rotation angle
transform.localEulerAngles = new Vector3(rotX, rotY, 0); // Apply rotation
player.Rotate(0, Input.GetAxis("Mouse X") * sensitivity, 0); // Apply left and right rotation to the player character
}
}
After writing the script, select the Main Camera object, click the Add Component button, and add the CameraController script you created.
Then drag the Player object to the Player property of the CameraController script to link it.
4. Testing and Adjusting
Now that all settings are complete, save the scene and click the play button to test the game.
You can see the camera rotating up and down as you move the mouse, and the player character rotating left and right.
If you want to adjust the sensitivity, you can change the value of the sensitivity variable to achieve your desired sensitivity.
5. Implementing Additional Features
In addition to basic up and down rotation, you can add more diverse features to provide a better user experience.
For example, you can use the SmoothDamp function to smooth the camera’s movement.
private Vector3 velocity = Vector3.zero; // Velocity variable
void LateUpdate()
{
Vector3 targetPosition = player.position; // Target position
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, 0.3f); // Smooth camera movement
}
This shows how to smoothly move the camera considering the speed between frames using the LateUpdate method.
This allows the camera to follow the player more naturally.
6. Conclusion
Through this lecture, we learned the basic method for rotating the camera’s view up and down in Unity and how to manipulate the player’s viewpoint.
Based on this foundation, you can utilize it for various game design and implementation.
Additionally, Unity offers many more features, so continuously learning and advancing is important.
In the future, we will cover various topics in the Unity beginner’s course series, so please stay tuned!
Happy Coding!