Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made a first person camera using parts of Brackeys Guide, but I have camera moving on both the axis but the line for the xRotation clamp doesnt work when I run the game. How can I fix this?


C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour
{   
    
        public float mouseSensitivity = 200f;

        public Transform playerBody;
        public Transform cam;
        private float xRotation = 0f;
        private float smoothSpeed = 5f;  // how smooth is camera movements
        
    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;     // stops you from click out off the sceen
    }
    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;    // mouse imput
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;    // mouse imput
    

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -80f, 73f);  // stops you from breaking your neck
        

        Quaternion localRotation = Quaternion.Euler(xRotation, 0f, 0f);  // left and right movement
        playerBody.Rotate(Vector3.up * mouseX);  // left and right movement 
        cam.Rotate(Vector3.left * mouseY);  // up and down movement
       
        
    }


What I have tried:

Setting new specific variables like roty and rotx, and clamping them individually.
Posted
Updated 26-Mar-22 16:36pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900