I have a question because I don't know how to return the rotation to the initial value after rotating the object.
For example, if you have a steering wheel object for a car,
code:
void rotate()
{
// Convert to -180 to 180
floatrotateY=(transform.eulerAngles.y>180)?transform.eulerAngles.y-360:transform.eulerAngles.y;
// 0.2 is a play value.
if(rotateY<=-0.2f)
{
transform.eulerAngles+ = new Vector3(0.0f, 20.0f, 0.0f) * Time.deltaTime;
}
else if (rotateY>=0.2f)
{
transform.eulerAngles - = new Vector3(0.0f, 20.0f, 0.0f) * Time.deltaTime;
}
}
After rotating the 90° object like this, the initial rotation value is returned.
At this rate, it will not exactly return to its initial value, and if it rotates faster, it will miss the play value.
Is there any standard way to write this kind of process?
unity3d
As differences accumulate, errors are inevitable, so the general rule is to calculate the displacement at each time.
For example, if you want to rotate by an angle だけ at time T0 and return it at a constant angular speed so that it becomes 0 at time T1, then each frame starts at time T (only the logic is indicated by the pseudo code)
if(T>=T1)
angle = 0
else
angle= **(T1-T)/(T1-T0)
and so on.In the actual implementation, you need to temporarily remember T0 and を, which are the starting points of the return animation, until the end of the animation.(Create a temporary object that represents "animated running" etc.)
© 2024 OneMinuteCode. All rights reserved.