본문 바로가기

카테고리 없음

Unity camera rotation script.

728x90

In Unity, a camera rotation script can be used to control the rotation of a camera in your game or application. The camera is an important component of any game or application that involves a 3D environment, as it determines what the player or user can see.

There are many ways to create a camera rotation script in Unity, but the basic concept is to use input from the user (e.g. mouse movement or touch input) to update the rotation of the camera. Here is an example of a simple camera rotation script in C#:

 

using UnityEngine;

public class CameraController : MonoBehaviour
{
    public float sensitivity = 5.0f;
    public float maxYAngle = 80.0f;
    private Vector2 currentRotation;

    void Update()
    {
        // Get mouse or touch input
        float mouseX = Input.GetAxis("Mouse X");
        float mouseY = -Input.GetAxis("Mouse Y");

        // Rotate camera based on input
        currentRotation.x += mouseX * sensitivity;
        currentRotation.y += mouseY * sensitivity;

        // Clamp y-axis rotation
        currentRotation.y = Mathf.Clamp(currentRotation.y, -maxYAngle, maxYAngle);

        // Apply rotation to camera transform
        transform.rotation = Quaternion.Euler(currentRotation.y, currentRotation.x, 0);
    }
}

In this script, the camera's rotation is updated every frame in the Update() method. The sensitivity variable determines how quickly the camera rotates in response to user input, while maxYAngle limits how far up or down the user can look. The currentRotation variable keeps track of the camera's current rotation in Euler angles, and is updated based on the user's input. Finally, the camera's transform is set to the new rotation using the Quaternion.Euler() method.

This is just a basic example, and there are many ways to customize and extend camera rotation scripts in Unity depending on your specific needs.

728x90