I need help witha bug,Glitch While looking up
when ever i look up it spazzes out and then the player controlls are backwards because the camera is backwards but i cant see whats wrong please help im new to coding C#
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerLook : MonoBehaviour { public Transform playerBody; public float mouseSensitivity;
 float xAxisClamp = 0.0f;
 void Awake() 
 {
     Cursor.lockState = CursorLockMode.Locked;
 }
 void Update() 
 {
     RotateCamera ();
 }
 void RotateCamera ()
 {
     float mouseX = Input.GetAxis ("Mouse X");
     float mouseY = Input.GetAxis ("Mouse Y");
     float rotAmountX = mouseX * mouseSensitivity;
     float rotAmountY = mouseY * mouseSensitivity;
     xAxisClamp -= rotAmountY;
     Vector3 targetRotCam = transform.rotation.eulerAngles;
     Vector3 targetRotBody = playerBody.rotation.eulerAngles;
     targetRotCam.x -= rotAmountY;
     targetRotCam.z = 0;
     targetRotBody.y += rotAmountX;
     if (xAxisClamp > 90) 
     {
         xAxisClamp = targetRotCam.x = 90;
     } else if (xAxisClamp < -90) 
     {
         xAxisClamp = -90;
     }
     transform.rotation = Quaternion.Euler (targetRotCam);
     playerBody.rotation = Quaternion.Euler (targetRotBody);
 }
 
               } ,Whenever i look up its spazzes out and then i walk backwards and i cant find anything wrong with the code CSHARP please help im new to coding
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerLook : MonoBehaviour { public Transform playerBody; public float mouseSensitivity;
 float xAxisClamp = 0.0f;
 void Awake() 
 {
     Cursor.lockState = CursorLockMode.Locked;
 }
 void Update() 
 {
     RotateCamera ();
 }
 void RotateCamera ()
 {
     float mouseX = Input.GetAxis ("Mouse X");
     float mouseY = Input.GetAxis ("Mouse Y");
     float rotAmountX = mouseX * mouseSensitivity;
     float rotAmountY = mouseY * mouseSensitivity;
     xAxisClamp -= rotAmountY;
     Vector3 targetRotCam = transform.rotation.eulerAngles;
     Vector3 targetRotBody = playerBody.rotation.eulerAngles;
     targetRotCam.x -= rotAmountY;
     targetRotCam.z = 0;
     targetRotBody.y += rotAmountX;
     if (xAxisClamp > 90) 
     {
         xAxisClamp = targetRotCam.x = 90;
     } else if (xAxisClamp < -90) 
     {
         xAxisClamp = -90;
     }
     transform.rotation = Quaternion.Euler (targetRotCam);
     playerBody.rotation = Quaternion.Euler (targetRotBody);
 }
 
               }
Your answer
 
             Follow this Question
Related Questions
Limit Z axis rotation on main camera (BUG?) 0 Answers
Camera always behind player 0 Answers
Code Optimization 1 Answer
How to focus camera on back of object when key is pressed. 0 Answers
Recognize when ever camera looks up and turns back down 0 Answers