- Home /
Clamping objects localrotation not working (C#)
I don't know why i'm having such a hard time with this it's a basic script. Basically I have a camera that i need to be able to orbit another object but only to an extent on the x axis it can circle it on y as much as it wants for all i care. But the x needs to stay between two values which I assigned via floats and tried restricting via Mathf.Clamp however the Vector3 does stay within the boundaries of the clamp when i apply it to the rotation it's as if it doesn't apply.
This is a child of an object i'm working with so I must use localRotation instead of rotation
Here is the script and to be more precise it's line 26 where it should update the objects localrotation but is not.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public Transform target;
public Camera camera;
public float minX;
public float maxX;
void Update()
{
//Allow Horizontal and Vertical Rotation to be controlled by the mouse
camera.transform.RotateAround (target.position, target.up, Input.GetAxis ("mouse_axis_0"));
camera.transform.RotateAround (target.position, target.right, Input.GetAxis ("mouse_axis_1"));
//Store the current rotation in euler angles
Vector3 localAngles = camera.transform.localEulerAngles;
//Make sure the the player cannot move the mouse to far horizotnally and vertically
localAngles.x = Mathf.Clamp (camera.transform.localRotation.eulerAngles.x, minX, maxX);
localAngles.z = 0f;
//Apply the rotation
camera.transform.localRotation = Quaternion.Euler (localAngles);
//Make sure the camera is looking at crash
camera.transform.LookAt (target);
}
}
Answer by b1gry4n · Oct 18, 2016 at 01:37 AM
the very last line of your update function tells the camera to lookat the target, everything youre doing before this is pretty much ignored because of that, you probably want to "lookat" before you do min/max checks.
Holy cow... this was actually the problem... I would have never expected this due to the camera going above the player while rotating around it, it was doing exactly as you explained bypassing the constraints.
Answer by YesNoKonrad · Oct 18, 2016 at 01:04 AM
My answer consists of 2 parts:
1) The script should work fine even though odd choices are made, I assume you did input degrees for the min and max values even though unity uses radians.
2) You may consider to cleanly calculate the new position, then clamp it, then transform it. That way you could easily extract code parts and reuse or replace them. It makes it also easier to read when you come back later. And believe me, you will come back later. And - assuming this script grows even a little bit - you'll be like: wtf is going on here.
Hope 1) helps :)
Unity doesn't use radians in most of it's API. Only their $$anonymous$$athf implementations use radians (since it mainly just wraps System.$$anonymous$$ath). All other methods of the actual API uses degrees. EulerAngles, RotateAround, Rotate, Vector3.Angle, ... all use degrees.
The actual problem is that Unity doesn't store the rotation as euler angles but as quaternion. Euler angles is an "over-specified" system so you can always have two different ways to express the exact same rotation. When Unity converts the rotation from it's quaternion representation into eulerangles you don't know which eulerangles representation you end up with. When you rotate beyond a certain point the conversion might suddenly return a different representation.
You should never do a read-modify-write operation on eulerangles as the eulerangles is not a reliable and continuous representation of the rotation.
So yes, the best way if you need $$anonymous$$ and max angles is to use your own variable to hold the current rotation and just apply it to the camera. That way you can ensure you always use the same euler angles representation.
$$anonymous$$eep in $$anonymous$$d that eulerangles suffer from gimbal lock while Quaternions do not.
I inputted a float value in the inspector x$$anonymous$$in = 0, x$$anonymous$$ax = 50. This was actually a cleaned up script from and old programmer trust me it looks a lot better than it did before. But I do think I just have the order a bit confused I shall try that.
Your answer
Follow this Question
Related Questions
Clamped rotation of turret problem 0 Answers
Problem with Mathf.Clamp and Mouse Follow Script 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why is my object's local x rotation not going past 90 degrees? 1 Answer