- Home /
How can I limit objects rotation to fixed range of 0 to 1? 0 being no rotation 1 full rotation?
I would like to control rotation with a range of 0 and 1. Whenever it's 0 then the object for rotation is at its default state and when the range is increased it starts rotating and when its at 1 it reaches full 360 rotation then resets to default … is this possible to do
having the range as float and having the variable adjustable for better control is what I'm trying to accomplish.
Is the object rotating at a set speed, that is increased by increasing the range, or is the object at a fixed rotation depending on the range value. What I mean is:
If the range is 0.5, the object takes 5 seconds to get to 360 degrees, whereas at 1, it only takes 2.5 seconds
Or, if the range is 0.5, the object's rotation on y axis is 180
If it's the first one, just multiply the speed of rotation by the range. If it is the second one, look into Quaternion.Euler, to set the rotation of the object depending on the range. $$anonymous$$g:
Object.transform.rotation = Quaternion.Euler(0, Range * 360, 0);
Answer by sath · Feb 06, 2021 at 03:39 PM
a draft solution to this would be something like
public float rotRange;//0-1
float resultRot;//0-360
float speed = 1f;
void Update()
{
//auto rotate test
//rotRange += Time.deltaTime * speed;
if (rotRange > 1 || rotRange < 0) rotRange = 0;
resultRot = rotRange * 360f;
transform.rotation = Quaternion.Euler(0, Math.Abs(resultRot), 0);
Debug.Log(resultRot);
}
just change the rotRange to see the result.
thank you! i haven't tried it yet, but its what i want and looks like it will do the job!