Hi So I have managed to clamp the rotation of an object which controls my camera, However, I have run into a strange issue. The clamp works when the rotation is in positive values, but as soon as it goes into the negative values it immediately snaps to the max angle for some reason.
Here is my code:
public GameObject target;
public float rotateSpeed = 5;
public float minConstraintsX, maxConstraintsX;
Vector3 offset;
void Start()
{
offset = target.transform.position - transform.position;
}
void LateUpdate()
{
float horizontal = Input.GetAxis("Horizontal") * rotateSpeed;
float vertical = Input.GetAxis("Vertical") * rotateSpeed;
target.transform.Rotate(0, horizontal, 0, Space.World);
target.transform.Rotate(vertical, 0, 0);
target.transform.localEulerAngles = new Vector3(Mathf.Clamp(target.transform.localEulerAngles.x, minConstraintsX, maxConstraintsX), target.transform.localEulerAngles.y, 0);
float desiredAngleY = target.transform.eulerAngles.y;
float desiredAngleX = target.transform.eulerAngles.x;
Quaternion rotation = Quaternion.Euler(desiredAngleX, desiredAngleY, 0);
transform.position = target.transform.position - (rotation * offset);
transform.LookAt(target.transform);
}
The Vector 3 is the issue I believe but I haven't run into something like this before.
Thanks in Advanced
↧