To create a mouse camera controller in Unity that includes zoom functionality using the mouse scroll wheel, you can modify the previous code to include zoom capabilities. Below is an extended version of the MouseCameraController which incorporates zoom functionality.
Complete Mouse Camera Controller with Zoom
-
Create a New Script (if you haven't yet):
- Name it
MouseCameraControllerWithZoom.cs.
-
Add the following code:
using UnityEngine;
public class MouseCameraControllerWithZoom : MonoBehaviour
{
public float mouseSensitivity = 100f; // Sensitivity of mouse movement
public Transform playerBody; // Reference to the player or object that the camera will rotate around
public float zoomSpeed = 2f; // Speed of zooming
public float minZoom = 15f; // Minimum zoom distance
public float maxZoom = 90f; // Maximum zoom distance
private float xRotation = 0f; // Rotation around the X axis
private float zoomLevel = 60f; // Default zoom level
void Start()
{
// Lock the cursor to the center of the screen and make it invisible
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
// Get mouse input for rotation
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
// Rotate the camera and the player
xRotation -= mouseY; // Look up and down
xRotation = Mathf.Clamp(xRotation, -90f, 90f); // Clamp the rotation
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX); // Rotate the player around the Y axis
// Zoom functionality
float scrollData = Input.GetAxis("Mouse ScrollWheel");
zoomLevel -= scrollData * zoomSpeed;
zoomLevel = Mathf.Clamp(zoomLevel, minZoom, maxZoom); // Clamp zoom level
// Apply zoom to the camera's position
Camera camera = GetComponent<Camera>();
camera.fieldOfView = zoomLevel; // Change the camera's field of view to implement zoom
}
}
Setup in Unity
-
Attach the Script:
- Drag and drop the
MouseCameraControllerWithZoom script onto your Camera GameObject in the Unity Inspector.
-
Assign Player Body:
- Drag your player GameObject (or a parent object) into the
playerBody field of the script in the Inspector to allow the camera to rotate around it.
-
Adjusting Parameters:
- You can modify:
mouseSensitivity to control the sensitivity of the camera rotation.
zoomSpeed to control how quickly the camera zooms in and out.
minZoom and maxZoom to set the limits for how close or how far the camera can zoom.
-
Testing:
- Play the scene. You should now be able to rotate your camera using the mouse and zoom in and out using the scroll wheel.
Additional Features
You can further expand this script with additional features, such as:
- Smooth Zooming: Implement interpolation for zooming, making it smoother.
- Inverted Y-axis: Provide an option to invert the Y-axis rotation.
- Zoom Levels: Use fixed zoom levels instead of a continuous zoom.
- Camera Constraints: Add limits or constraints based on your game mechanics.
With this script and setup, you should have a functional mouse camera controller that can look around and zoom in and out!