Search Unity

Question How can I limit the rotation on the Y axis so the player cant spin the camera 360 in an FPS game?

Discussion in 'Scripting' started by bokivasoski, Sep 13, 2020.

  1. bokivasoski

    bokivasoski

    Joined:
    Apr 2, 2020
    Posts:
    3
    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.
    Code (CSharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. public class CharController_Motor : MonoBehaviour {
    6.      private float speed = 10.0f;
    7.      public float walkSpeed=7.0f;
    8.      public float runSpeed = 12.0f;
    9.      public float sensitivity = 30.0f;
    10.      public float WaterHeight = 15.5f;
    11.      CharacterController character;
    12.      private GameObject cam;
    13.      private NavMeshAgent nav;
    14.      private bool AIActive = true;
    15.      public GameObject cam1;
    16.      public GameObject cam2;//Nightvision
    17.      //private bool NightVision = false;
    18.      float moveFB, moveLR;
    19.      float rotX, rotY;
    20.    
    21.      float gravity = -9.8f;
    22.      public float Stamina = 10f;
    23.      public float MaxStamina = 10f;
    24.      public int DecayRate = 1;
    25.      public float RefillRate=0.25f;
    26.      public GameObject LightBreathing;
    27.      public GameObject HeavyBreathing;
    28.      private bool LightBreath = false;
    29.      private bool HeavyBreath = false;
    30.      public GameObject LowHealthSound;
    31.      public GameObject PlayerDeath;
    32.      public GameObject ChaseMusic;
    33.      void Start(){
    34.          //LockCursor ();
    35.    
    36.          character = GetComponent<CharacterController> ();
    37.          SaveScript.PlayerHealth = 30;
    38.    
    39.            
    40.              sensitivity = sensitivity * 1.5f;
    41.              speed = walkSpeed;
    42.              Cursor.visible = false;
    43.              cam = cam1;
    44.              cam2.gameObject.SetActive(false);
    45.              LightBreathing.gameObject.SetActive(false);
    46.              HeavyBreathing.gameObject.SetActive(false);
    47.              nav = GetComponent<NavMeshAgent>();
    48.              AIActive = true;
    49.              LowHealthSound.gameObject.SetActive(false);
    50.              PlayerDeath.gameObject.SetActive(false);
    51.              ChaseMusic.gameObject.SetActive(false);
    52.      }
    53.      void CheckForWaterHeight(){
    54.          if (transform.position.y < WaterHeight) {
    55.              gravity = 0f;          
    56.          } else {
    57.              gravity = -9.8f;
    58.          }
    59.      }
    60.      void Update(){
    61.          if (LightBreath == false)
    62.          {
    63.              if (Stamina < 3)
    64.              {
    65.                  LightBreathing.gameObject.SetActive(true);
    66.                  HeavyBreathing.gameObject.SetActive(false);
    67.                  LightBreath = true;
    68.              }
    69.          }
    70.          if (LightBreath == true)
    71.          {
    72.              if (Stamina > 3)
    73.              {
    74.                  LightBreathing.gameObject.SetActive(false);
    75.                  HeavyBreathing.gameObject.SetActive(false);
    76.                  LightBreath = false;
    77.              }
    78.          }
    79.          if (HeavyBreath == false)
    80.          {
    81.              if (Stamina == 0)
    82.              {
    83.                  LightBreathing.gameObject.SetActive(false);
    84.                  HeavyBreathing.gameObject.SetActive(true);
    85.                  HeavyBreath = true;
    86.              }
    87.          }
    88.          if (HeavyBreath == true)
    89.          {
    90.              if (Stamina > 0)
    91.              {
    92.                  LightBreathing.gameObject.SetActive(false);
    93.                  HeavyBreathing.gameObject.SetActive(false);
    94.                  HeavyBreath = false;
    95.              }
    96.          }
    97.          if (Stamina > 0)
    98.          {
    99.              if (Input.GetButton("Run"))
    100.              {
    101.                  speed = runSpeed;
    102.                  Stamina = Stamina - DecayRate * Time.deltaTime;
    103.                  if (Stamina < 0)
    104.                  {
    105.                      Stamina = 0;
    106.                  }
    107.                
    108.              }
    109.              else
    110.              {
    111.                  speed = walkSpeed;
    112.                  Stamina = Stamina + RefillRate * Time.deltaTime;
    113.                  if (Stamina > MaxStamina)
    114.                  {
    115.                      Stamina = MaxStamina;
    116.                  }
    117.              }
    118.          }
    119.          if (Stamina == 0)
    120.          {
    121.              speed = walkSpeed;
    122.              StartCoroutine(StaminaRefill());
    123.          }
    124.          if (SaveScript.PlayerHealth < 5)
    125.          {
    126.              LowHealthSound.gameObject.SetActive(true);
    127.          }
    128.          if (SaveScript.PlayerHealth > 4)
    129.          {
    130.              LowHealthSound.gameObject.SetActive(false);
    131.          }
    132.          if(SaveScript.PlayerHealth <= 0)
    133.          {
    134.              PlayerDeath.gameObject.SetActive(true);
    135.          }
    136.          if (AIActive == true)
    137.          {
    138.              nav.enabled = true;
    139.          }
    140.          else
    141.          {
    142.              nav.enabled = false;
    143.          }
    144.          if (SaveScript.NightVision==false)
    145.          {
    146.              cam = cam1;
    147.              cam2.gameObject.SetActive(false);//Deactivate NightVision
    148.              SaveScript.NightVision = false;
    149.          }
    150.          if (Input.GetKeyDown(KeyCode.N))
    151.              {
    152.              if (SaveScript.NightVision == false)
    153.              {
    154.                  cam2.gameObject.SetActive(true);//Activate NightVision
    155.                  cam = cam2;
    156.                  SaveScript.NightVision = true;
    157.              }
    158.              else
    159.              {
    160.                  cam = cam1;
    161.                  cam2.gameObject.SetActive(false);//Deactivate NightVision
    162.                  SaveScript.NightVision = false;
    163.              }
    164.          }
    165.          moveFB = Input.GetAxis ("Horizontal") * speed;
    166.          moveLR = Input.GetAxis ("Vertical") * speed;
    167.          rotX = Input.GetAxis ("Mouse X") * sensitivity;
    168.          rotY = Input.GetAxis ("Mouse Y") * sensitivity;
    169.        
    170.          //rotX = Input.GetKey (KeyCode.Joystick1Button4);
    171.          //rotY = Input.GetKey (KeyCode.Joystick1Button5);
    172.          CheckForWaterHeight ();
    173.          Vector3 movement = new Vector3 (moveFB, gravity, moveLR);
    174.          CameraRotation(cam, rotX, rotY);
    175.        
    176.          movement = transform.rotation * movement;
    177.          character.Move (movement * Time.deltaTime);
    178.    
    179.      }
    180.      void CameraRotation(GameObject cam, float rotX, float rotY){      
    181.          transform.Rotate (0, rotX * Time.deltaTime, 0);
    182.          cam.transform.Rotate (-rotY * Time.deltaTime, 0, 0);
    183.        
    184.      }
    185.      private void OnTriggerEnter(Collider other)
    186.      {
    187.          if (other.gameObject.CompareTag("Water"))
    188.          {
    189.              AIActive = false;
    190.          }
    191.      }
    192.      private void OnTriggerExit(Collider other)
    193.      {
    194.          if (other.gameObject.CompareTag("Water"))
    195.          {
    196.              AIActive = true;
    197.          }
    198.      }
    199.      IEnumerator StaminaRefill()
    200.      {
    201.          yield return new WaitForSeconds(MaxStamina);//Wait 10 seconds before setting stamina to max
    202.          if (Stamina == 0)
    203.          {
    204.              Stamina = MaxStamina;
    205.          }
    206.      }
    207. }
    208.  
     

    Attached Files:

  2. bokivasoski

    bokivasoski

    Joined:
    Apr 2, 2020
    Posts:
    3
    EDIT: I want to limt how much the player can look down and up.
     
  3. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Uhh , you have many repeated code , and not clean code, so just in case your code get reviewed , make sure you clean it up!

    to limit the Y or X Axis you get the Y or X axis from the Camera and set a limit to that
    for example:
    Code (CSharp):
    1. rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
     
  4. bokivasoski

    bokivasoski

    Joined:
    Apr 2, 2020
    Posts:
    3
    Can't get this to work...