- Home /
Question by
unity_w108xjlENsUk1Q · Apr 28, 2020 at 01:07 AM ·
c#rotationrotateangle
How to rotate an object without using a timer
Hello! The object must rotate first in one direction, then in the other. I wrote such a script, but I think it is possible without a timer. How can this functionality be implemented using a Quaternion and angle comparison?
float right = 45f;
float left = -45f;
void Update () {
if (_timerRotate)
{
_timer += Time.deltaTime;
if(_timer < 1.5f)
{
angle = right;
}
else { angle = left; }
if(_timer >= 4.5f) { angle = right; if (_timer > 6f) _timer = 0; }
}
transform.Rotate(0, angle * Time.deltaTime, 0);
}
Comment
Answer by unity_w108xjlENsUk1Q · Apr 28, 2020 at 02:22 PM
Thanks for the help. I do not need a time reference. Is this a normal code or is there an error somewhere? It works as it should.
currentEulerAngles += new Vector3(0, angle, 0) * Time.deltaTime;
if (currentEulerAngles.y > 45f)
{
angle = left;
}
if (currentEulerAngles.y <= -45f)
{
angle = right;
}
transform.Rotate(0, angle * Time.deltaTime, 0);
Ah very nice! It looks like it should work. I would move the first * Time.deltaTime to inside the Vector3 in case it does unnecessary multiplication by the two 0's and 'else if' on the 2nd if statement.