- Home /
,How to clamp my turret rotation?
][1]][1] I have a turret that looks to the enemy object it works perfectly, but I don't know how to clamp the rotation please help!
void Update ()
{
if (target == null)
return;
Vector3 dir = target.position - xPivot.position;
Quaternion lookRotation = Quaternion.LookRotation(dir);
Vector3 yRotation = Quaternion.Lerp(yPivot.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles;
Vector3 xRotation = Quaternion.Lerp(xPivot.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles;
yPivot.rotation = Quaternion.Euler(0f, yRotation.y, 0f);
xPivot.rotation = Quaternion.Euler(xRotation.x, yRotation.y, 0f);
}
$$anonymous$$Aybe splitting the vector yRotation in 3 integrers, and, after seting them, check if > maximum or < $$anonymous$$imum
Answer by Magso · Mar 01, 2020 at 09:32 PM
The axes' x,y and z can be clamped individually before the rotation is set for e.g.
yRotation.y = Mathf.Clamp(yRotation.y, min, max);
yPivot.rotation = Quaternion.Euler(0f, yRotation.y, 0f);
Hi $$anonymous$$agso, it works I clamped the xRotation but why this is happen?
This can happen if the clamp's $$anonymous$$ and max are the wrong way round, such as (-2, -10)
ins$$anonymous$$d of (-10, -2)
. If this isn't the problem are there any other changes you've made since?
i know that -10 < -2
but my turret rotation is 360 degrees here's the debug when my enemy is below and above.
Your answer
Follow this Question
Related Questions
How to properly reference a Euler? 1 Answer
Rotation on Android vs. PC 0 Answers
Compare euler angles to check if they describe the same rotation. 1 Answer
How to fix flipping Euler anlges? 0 Answers
Can't Accurately Read Rotations 0 Answers