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): void Update () { if (transform.eulerAngles.x >= 346 && transform.eulerAngles.x <= 348 ) { transform.eulerAngles.x = 348; Debug.Log(" camera should stop now "); } } Assets/Scripts/BoundCamera.cs(5,31): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'
I guess you did something like this? Code (JavaScript): xRotation = Mathf.Clamp(xRotation, -90, 90); yRotation = Mathf.Clamp(yRotation, -75, 75); If you figured it out tell others. Because some people may have same issues as you.
Code (CSharp): using UnityEngine; using System.Collections; public class BoundCamera : MonoBehaviour { Vector3 angles; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (transform.eulerAngles.x >= 344 && transform.eulerAngles.x <= 346 ) { angles = gameObject.transform.eulerAngles; angles.x = 346; transform.eulerAngles = angles; Debug.Log(" camera should stop now "); } if (transform.eulerAngles.x >= 3.026316f && transform.eulerAngles.x <= 6 ) { angles = gameObject.transform.eulerAngles; angles.x = 3.026316f; transform.eulerAngles = angles; Debug.Log(" camera should stop now "); } } }
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): angles.x = Mathf.Clamp(angles.x, min, max); 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.