I want to make it so i can’t move the camera 360 degrees but when i try to clamp it just clamps on the side i tried both Y and X,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCameraController : MonoBehaviour
{
//Varibales
public Transform player;
float Sensitivity = 2f;
float XMouse = 0f;
float YMouse = 0f;
// Start is called before the first frame update
void Start()
{
//Cursor Options
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
// Gets position of mouse cursor
XMouse += Input.GetAxis("Mouse X") * Sensitivity;
YMouse -= Input.GetAxis("Mouse Y") * Sensitivity;
// Transform camera
transform.localEulerAngles = new Vector3(YMouse, 0, 0);
//Transform Player with camera
player.localEulerAngles = new Vector3(0, XMouse, 0);
//Clamps the rotation
XMouse = Mathf.Clamp(XMouse, -60, 45);
}
}
You’re always gonna have gimbal lock issues if you fiddle with .eulerAngles. Here’s why:
https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html
Camera stuff is pretty tricky, especially with clamping and bounding… you may wish to consider using Cinemachine from the Unity Package Manager.
When you read the euler angle of the camera, it’s going to be between 0 and 360. If you want to clamp from a negative number, you’re going to have to jump through some hoops.
I Diden’t need to jump through any hoops i just clamped the y rotation even if the camera is rotation on the x rotation which is kinda werid but it works and if it works it work Thx for the help and here is my code if you want to see it =)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCameraController : MonoBehaviour
{
//Varibales
public Transform player;
float Sensitivity = 2f;
float XMouse = 0f;
float YMouse = 0f;
// Start is called before the first frame update
void Start()
{
//Cursor Options
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
// Gets position of mouse cursor
XMouse += Input.GetAxis("Mouse X") * Sensitivity;
YMouse -= Input.GetAxis("Mouse Y") * Sensitivity;
// Transform camera
transform.localEulerAngles = new Vector3(YMouse, 0, 0);
//Transform Player with camera
player.localEulerAngles = new Vector3(0, XMouse, 0);
//Clamps the rotation
YMouse = Mathf.Clamp(YMouse, -30f, 30f);
}
}