Camera Boundaries Question

Hello Unitoracle -

I’m trying to get the following chunk of code functioning such that when the user moves the joystick, the camera pivots only within a set boundary. It’s behaving as-expected when I move the stick up, down and to the right.

So, oh wise and powerful Unitoracle, why oh why is this not working when I move to the left? Instead, it’s “bouncing.”

[AddComponentMenu("Camera-Control/Stick Look")]
public class StickLook : MonoBehaviour {

    public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityX = 15F;
    public float sensitivityY = 15F;

    public float minimumX = -360F;
    public float maximumX = 360F;

    public float minimumY = -360F;
    public float maximumY = 360F;

    float rotationY = 0F;

    void Update ()
    {
                if (axes == RotationAxes.MouseXAndY) {
                        float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Horizontal") * sensitivityX;
           
                        rotationY += Input.GetAxis ("Vertical") * sensitivityY;
                        rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

                        rotationX += Input.GetAxis ("Horizontal") * sensitivityX;
                        rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
       
           
                        transform.localEulerAngles = new Vector3 (rotationY, rotationX, 0);
                }

        else if (axes == RotationAxes.MouseX)
        {
            transform.Rotate(0, Input.GetAxis("Horizontal") * sensitivityX, 0);
        }
        else
        {
            rotationY += Input.GetAxis("Vertical") * sensitivityY;
            rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
           
        transform.localEulerAngles = new Vector3(rotationY, transform.localEulerAngles.y, 0);
        }
}
   
    void Start ()
    {
        // Make the rigid body not change rotation
        if (GetComponent<Rigidbody>())
            GetComponent<Rigidbody>().freezeRotation = true;
    }
}

Bumping because I’m still hella confused…

Bumping - I’ll paypal $10 to whoever can help me with this, Google is letting me down :frowning:

using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Stick Look")]
public class StickLook : MonoBehaviour {

    public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityX = 15F;
    public float sensitivityY = 15F;

    public float minimumX = -360F;
    public float maximumX = 360F;

    public float minimumY = -360F;
    public float maximumY = 360F;

    float rotationY = 0F;
    float rotationX = 0f; // Keep track of your own x rotation, like with the y rotation.

    void Update ()
    {
        if (axes == RotationAxes.MouseX || axes == RotationAxes.MouseXAndY)
        { 
            rotationX += Input.GetAxis ("Horizontal") * sensitivityX;
            rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
        }
        if (axes == RotationAxes.MouseY || axes == RotationAxes.MouseXAndY)
        {
            rotationY += Input.GetAxis ("Vertical") * sensitivityY;
            rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
        }
        // Always set to the x and y rotations you control.
        // All unity rotations are stored as Quaternions, and you cannot trust them to return the same euler angles you feed them.
        transform.localEulerAngles = new Vector3 (rotationY, rotationX, 0); 
    }
   
    void Start ()
    {
        // Make the rigid body not change rotation
        if (GetComponent<Rigidbody>())
            GetComponent<Rigidbody>().freezeRotation = true;
    }

    void OnEnable() {
        // Get the starting here if you really have to...
        rotationY = transform.localEulerAngles.x;
        rotationX = transform.localEulerAngles.y;
    }
}

Awesome! I’ll try this when I get home, PM me your email…

That’s ok :slight_smile:
The reason you were getting that behaviour was because Unity stores rotations as Quaternsions.
This means that every time you try to get or try to set euler angles Unity will have to convert it from and to a Quaternion, It never stores the euler angles.
Take a look at the documentation for some additional information:

Thanks! We’re pretty new to this, so this can be tough info to track down - like a needle in a haystack. Thanks very much for explaining!