Search Unity

Stop camera from rotating

Discussion in 'Scripting' started by caseyboundless, Sep 1, 2014.

  1. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    When I rotate my player(camera) it looks up and down too far. I want to limit this. Here is my code that checks the rotation. I don't know how to stop it once my if statement is true? So the player can't look up or down anymore and has to move the opposite way.


    Code (CSharp):
    1. void Update ()
    2.     {
    3.  
    4.         if (transform.eulerAngles.x >= 346 && transform.eulerAngles.x <= 348 )
    5.         {
    6.  
    7.             transform.eulerAngles.x = 348;
    8.             Debug.Log(" camera should stop now ");
    9.         }
    10.  
    11.  
    12.  
    13.     }

    Assets/Scripts/BoundCamera.cs(5,31): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'
     
  2. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    figured it out
     
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    I guess you did something like this?

    Code (JavaScript):
    1. xRotation = Mathf.Clamp(xRotation, -90, 90);
    2. yRotation = Mathf.Clamp(yRotation, -75, 75);
    If you figured it out tell others. Because some people may have same issues as you.
     
  4. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BoundCamera : MonoBehaviour
    5. {
    6.     Vector3 angles;
    7.    
    8.     // Use this for initialization
    9.     void Start () {
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update ()
    14.     {
    15.         if (transform.eulerAngles.x >= 344 && transform.eulerAngles.x <= 346 )
    16.         {
    17.             angles = gameObject.transform.eulerAngles;
    18.             angles.x = 346;
    19.             transform.eulerAngles = angles;
    20.             Debug.Log(" camera should stop now ");
    21.         }
    22.    
    23.  
    24.         if (transform.eulerAngles.x >= 3.026316f && transform.eulerAngles.x <= 6 )
    25.         {
    26.             angles = gameObject.transform.eulerAngles;
    27.             angles.x = 3.026316f;
    28.             transform.eulerAngles = angles;
    29.             Debug.Log(" camera should stop now ");
    30.         }
    31.    
    32.     }
    33. }
     
    elmar1028 likes this.
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Thanks. However, you could achieve same results with less coding.
    Now try out my code. Mathf.Clamp limits the numbers(min max) of the variable.

    So instead of if statements put these two in the update function:

    Code (CSharp):
    1. angles.x = Mathf.Clamp(angles.x, min, max);
    2. angles.y  = Mathf.Clamp(angles.y, min, max);
    Instead of "min/max" put a minimum rotation number and maximum rotation number you want your camera to rotate.
     
  6. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    I'll try it out.