Hi,
I’m using Quaternion.Slerp to rotate an object smoothly on one single axis, following a target.
Now I want to limit the range it can move on the X axis.
var direction = (primaryTarget.position - bridge.position).normalized;
var newRotation = Quaternion.LookRotation(direction);
newRotation = Quaternion.Euler(newRotation.eulerAngles.x, bridge.rotation.eulerAngles.y,
bridge.rotation.eulerAngles.z);
bridge.rotation = Quaternion.Slerp(bridge.rotation, newRotation, movementSpeed);
I’ve tried clamping the value to no avail. Any suggestions would be greatly appreciated.
How exactly do you need to clamp it to the desired window? The rigorous approach would be to keep trying Quaternion.RotateTowards, and then if the result.eulerAngles is outside your proscribed range, try it again with a smaller max rotation value. A heuristic to guess at the best initial max rotation value would be to calculate an approximate Euler rotation for the boundary first.
The context is this object needs to rotate smoothly toward a moving target on only the X axis, but it can only rotate 8~ degrees either direction. I have the rotating working, I just can’t figure out how I’m supposed to clamp the axis. I can try RotateTowards instead of LookRotation next.
This is linear.
Count how many you need.
You need 360 / 8 = 45 facings
An Euler angle axis can be a combination of two axis’ working in sync, or it can be set.
You are allowed to set the X euler using an = (int) variable. But it must be a variable. The result must be set and it cannot be a reference to the int of an Euler angle. The result also cannot be created using +/- within a vector without encountering the aforementioned lock. Thus you cannot equate intervals such as 8 x 4 = my Eulers vector X. You can only tell the Euler what it needs to be.
This is a snippet from my Project that handles a rotating Euler on an items prop. This is set by any single axis by a specified file. PROP_ROT = int[ ]; // of all my props eulers in a single list
for (i = 0; i < UD.PROPS.Count; i++)
{
X = UD.PROP_ROT[0];
Y = UD.PROP_ROT[1];
Z = UD.PROP_ROT[2];
if (UD.PROP_AXIS[i].x > 0)
UD.PROP_ROT[0 + (3 * i)] += 1;
if (UD.PROP_AXIS[i].y > 0)
UD.PROP_ROT[1 + (3 * i)] += 1;
if (UD.PROP_AXIS[i].z > 0)
UD.PROP_ROT[2 + (3 * i)] += 1;
if (UD.PROP_ROT[0 + (3 * i)] >= 360)
UD.PROP_ROT[0 + (3 * i)] = 0;
if (UD.PROP_ROT[1 + (3 * i)] >= 360)
UD.PROP_ROT[1 + (3 * i)] = 0;
if (UD.PROP_ROT[2 + (3 * i)] >= 360)
UD.PROP_ROT[2 + (3 * i)] = 0;
UD.PROPS[i].transform.localEulerAngles = new Vector3(UD.PROP_ROT[0 + (3 * i)], UD.PROP_ROT[1 + (3 * i)], UD.PROP_ROT[2 + (3 * i)]);
}