- Home /
Question by
Vadom6 · Dec 19, 2019 at 11:28 PM ·
rotationvector3camera-movementclamp
Clamping rotation of an object
Hi I am trying to create a basic camera system which at the moment moves the way I want it to except for the x axis going to high and to low in relation to the camera target. How would I be able to clamp the x axis. I am using a secondary objects rotation to position my camera so I know I have to clamp how far that object can rotate but I am unsure how to.
I wish to use the constraintsX float to be the value for the clamp.
Here is my code:
public GameObject target;
public float rotateSpeed = 5;
public float constraintsX;
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);
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);
//Property of Vadom
}
Thanks In Advance for your help
Comment
Your answer
Follow this Question
Related Questions
Clamping Vector3 2 Answers
Camera Clamping Third Person? 0 Answers
Rotate Vector3 array around a point 1 Answer
How to get a proper ramming effect? 1 Answer