Skip to content

Instantly share code, notes, and snippets.

@marcusx2
Last active February 26, 2024 16:51
Show Gist options
  • Save marcusx2/8563e00d6fec2ecb69b2f8d6dd8d2435 to your computer and use it in GitHub Desktop.
Save marcusx2/8563e00d6fec2ecb69b2f8d6dd8d2435 to your computer and use it in GitHub Desktop.
Look around camera with mouse - Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FreeCamera : MonoBehaviour
{
public float sensitivity = 10f;
public float maxYAngle = 80f;
private Vector2 currentRotation;
void Update()
{
currentRotation.x += Input.GetAxis("Mouse X") * sensitivity;
currentRotation.y -= Input.GetAxis("Mouse Y") * sensitivity;
currentRotation.x = Mathf.Repeat(currentRotation.x, 360);
currentRotation.y = Mathf.Clamp(currentRotation.y, -maxYAngle, maxYAngle);
Camera.main.transform.rotation = Quaternion.Euler(currentRotation.y,currentRotation.x,0);
if (Input.GetMouseButtonDown(0))
Cursor.lockState = CursorLockMode.Locked;
}
}
@marcusx2
Copy link
Author

A simple script to look around with the game's camera using the mouse. Useful to debug on the game view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment