- Home /
 
               Question by 
               bokivasoski · Sep 16, 2020 at 07:57 PM · 
                c#cameracharactercharactercontroller  
              
 
              How can I limit the rotation on the Y axis so the player cant spin the camera 360 in an FPS game?
I have an upcoming project that I have to present on Monday an this is the last bug I have to resolve. It would be nice if someone helped me out and teach me how to apply the axis limiter. Thanks in advance everyone.
 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.AI;
  
  public class CharController_Motor : MonoBehaviour {
  
      private float speed = 10.0f;
  
      public float walkSpeed=7.0f;
      public float runSpeed = 12.0f;
  
      public float sensitivity = 30.0f;
      public float WaterHeight = 15.5f;
      CharacterController character;
      private GameObject cam;
  
      private NavMeshAgent nav;
      private bool AIActive = true;
  
      public GameObject cam1;
      public GameObject cam2;//Nightvision
      //private bool NightVision = false;
  
  
  
  
      float moveFB, moveLR;
      float rotX, rotY;
      
  
      float gravity = -9.8f;
  
      public float Stamina = 10f;
      public float MaxStamina = 10f;
      public int DecayRate = 1;
      public float RefillRate=0.25f;
  
      public GameObject LightBreathing;
      public GameObject HeavyBreathing;
      private bool LightBreath = false;
      private bool HeavyBreath = false;
  
  
      public GameObject LowHealthSound;
  
  
  
      public GameObject PlayerDeath;
  
  
      public GameObject ChaseMusic;
      void Start(){
          //LockCursor ();
      
          character = GetComponent<CharacterController> ();
          SaveScript.PlayerHealth = 30;
      
              
              sensitivity = sensitivity * 1.5f;
              speed = walkSpeed;
              Cursor.visible = false;
  
              cam = cam1;
              cam2.gameObject.SetActive(false);
              LightBreathing.gameObject.SetActive(false);
              HeavyBreathing.gameObject.SetActive(false);
              nav = GetComponent<NavMeshAgent>();
              AIActive = true;
              LowHealthSound.gameObject.SetActive(false);
              PlayerDeath.gameObject.SetActive(false);
              ChaseMusic.gameObject.SetActive(false);
  
      }
  
  
      void CheckForWaterHeight(){
          if (transform.position.y < WaterHeight) {
              gravity = 0f;            
          } else {
              gravity = -9.8f;
          }
      }
  
  
  
      void Update(){
          if (LightBreath == false)
          {
              if (Stamina < 3)
              {
                  LightBreathing.gameObject.SetActive(true);
                  HeavyBreathing.gameObject.SetActive(false);
                  LightBreath = true;
              }
          }
          if (LightBreath == true)
          {
              if (Stamina > 3)
              {
                  LightBreathing.gameObject.SetActive(false);
                  HeavyBreathing.gameObject.SetActive(false);
                  LightBreath = false;
              }
          }
          if (HeavyBreath == false)
          {
              if (Stamina == 0)
              {
                  LightBreathing.gameObject.SetActive(false);
                  HeavyBreathing.gameObject.SetActive(true);
                  HeavyBreath = true;
              }
          }
          if (HeavyBreath == true)
          {
              if (Stamina > 0)
              {
                  LightBreathing.gameObject.SetActive(false);
                  HeavyBreathing.gameObject.SetActive(false);
                  HeavyBreath = false;
              }
          }
          if (Stamina > 0)
          {
  
              if (Input.GetButton("Run"))
              {
                  speed = runSpeed;
                  Stamina = Stamina - DecayRate * Time.deltaTime;
                  if (Stamina < 0)
                  {
                      Stamina = 0;
                  }
                  
              }
              else
              {
                  speed = walkSpeed;
                  Stamina = Stamina + RefillRate * Time.deltaTime;
                  if (Stamina > MaxStamina)
                  {
                      Stamina = MaxStamina;
                  }
              }
          }
          if (Stamina == 0)
          {
              speed = walkSpeed;
              StartCoroutine(StaminaRefill());
          }
          if (SaveScript.PlayerHealth < 5)
          {
              LowHealthSound.gameObject.SetActive(true);
          }
          if (SaveScript.PlayerHealth > 4)
          {
              LowHealthSound.gameObject.SetActive(false);
          }
          if(SaveScript.PlayerHealth <= 0)
          {
              PlayerDeath.gameObject.SetActive(true);
          }
  
  
  
          if (AIActive == true)
          {
              nav.enabled = true;
          }
          else 
          {
              nav.enabled = false;
          }
          if (SaveScript.NightVision==false)
          {
              cam = cam1;
              cam2.gameObject.SetActive(false);//Deactivate NightVision
              SaveScript.NightVision = false;
          }
          if (Input.GetKeyDown(KeyCode.N))
              {
              if (SaveScript.NightVision == false)
              {
                  cam2.gameObject.SetActive(true);//Activate NightVision
                  cam = cam2;
                  SaveScript.NightVision = true;
              }
              else
              {
                  cam = cam1;
                  cam2.gameObject.SetActive(false);//Deactivate NightVision
                  SaveScript.NightVision = false;
              }
          }
  
          moveFB = Input.GetAxis ("Horizontal") * speed;
          moveLR = Input.GetAxis ("Vertical") * speed;
  
          rotX = Input.GetAxis ("Mouse X") * sensitivity;
          rotY = Input.GetAxis ("Mouse Y") * sensitivity;
          
  
          //rotX = Input.GetKey (KeyCode.Joystick1Button4);
          //rotY = Input.GetKey (KeyCode.Joystick1Button5);
  
          CheckForWaterHeight ();
  
  
          Vector3 movement = new Vector3 (moveFB, gravity, moveLR);
  
  
  
          CameraRotation(cam, rotX, rotY);
          
          movement = transform.rotation * movement;
          character.Move (movement * Time.deltaTime);
      
  
      }
  
  
      void CameraRotation(GameObject cam, float rotX, float rotY){        
          transform.Rotate (0, rotX * Time.deltaTime, 0);
          cam.transform.Rotate (-rotY * Time.deltaTime, 0, 0);
          
      }
  
      private void OnTriggerEnter(Collider other)
      {
          if (other.gameObject.CompareTag("Water"))
          {
              AIActive = false;
          }
      }
      private void OnTriggerExit(Collider other)
      {
          if (other.gameObject.CompareTag("Water"))
          {
              AIActive = true;
          }
      }
      IEnumerator StaminaRefill()
      {
          yield return new WaitForSeconds(MaxStamina);//Wait 10 seconds before setting stamina to max
          if (Stamina == 0)
          {
              Stamina = MaxStamina;
          }
      }
  
  }
  
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
Object reference not set to an instance of an Object 1 Answer
OnCollisionEnter not working 1 Answer
Help!Crash! FatalError"Callback registration failed kMaxCallback" 1 Answer
Third person camera; player rotation. 1 Answer
How to reset character orientation based on direction the camera is facing 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                