- Home /
Rotating Rigidbodies
Say that I have square and I have to rotate it every frame (or physics update) to 90 degrees on the z axis all in a fixed time (1 second). How do I do it?
Can you please clarify, "to 90 degrees" or "by 90 degrees" each frame. Those are very different requirements
To 90 degrees. The square should be rotated by 90 degrees at the end of a second
Answer by ozrfrkan · Oct 14, 2020 at 12:07 PM
public Transform target;
public float speed = 1.0f;
void Update()
{
Vector3 targetDirection = target.position - transform.position;
float singleStep = speed * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
transform.rotation = Quaternion.LookRotation(newDirection);
}
Look this script, it can be useful to animating the rotation per second.
The 'single step' is what I needed. In my case that would be (untested) :
private float singleStep;
public float speed = -90;
void FixedUpdate()
{
singleStep = speed * Time.deltaTime;
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z + singleStep);
}
thancc
Your answer
Follow this Question
Related Questions
GUI elements vanish when publishing 1 Answer
Timer Between Labels 2 Answers
Need some help an object collider that stops a timer then displays it on another scene 1 Answer
countdown/countup timer 1 Answer
Timer Pause 1 Answer