Public Group

# How do I rotate an object back and forth?

This topic is 626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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?

##### 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

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633767
• Total Posts
3013734
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!