• Advertisement
Sign in to follow this  

How do I rotate an object back and forth?

Recommended Posts

I want my object to rotate between two angles in relation to speed and it doesn't need to be a smooth rotation, how can I achieve that? 

Thank you in advance.


Share this post

Link to post
Share on other sites
Like a speedometer needle?

When the speed changes, do the following:
float normalizedSpeed = actualSpeed / kMaximumSpeedForSpeedometer;

if (normalizedSpeed < 0.0f) // You can also use Mathf.clamp if you want.
   normalizedSpeed = 0.0f;
else if (normalizedSpeed > 1.0f)
   normalizedSpeed = 1.0f;

float angleForSpeedometer = normalizedSpeed * (maximumAngle - minimumAngle) + minimumAngle;

// set the speedometer needle's transform rotation using this angle.
// see: https://docs.unity3d.com/ScriptReference/Transform-rotation.html
// or: https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html
maximumAngle is a rotation angle that you specify for the top-speed of the needle. minimumAngle is the same thing but for the zero-speed angle. Edited by Nypyren

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Advertisement