Stop Animation in an exact rotation

Hi All :slight_smile:

I’m trying to create a kind of slot (but not a slot machine). I created an animated object that is started and stopped with a button. the animation object starts and stops, but not with a certain value (36 degrees). How to do?

Else. do you have tips on how to enter the number that comes out after a rotation stop in a text? I know how to do it, but the numbers are engraved on a texture, and I can’t figure out which way to go.
I need this example for the OGM game level which is part of an enigma …

I have Attached a package containing everything you’ve need (including the script) that I use for this test

Thank you all for any information and suggestions!

5069771–498431–TestRotate.unitypackage (155 KB)

It’s going to be very tough to get an animation to stop at a specific degree point (unless the animation clip itself stops at that point), but you really don’t need to use an animation at all for this. All you’re doing is rotating in a single axis, which is dead simple to rotate in code, and you can much more easily round to the nearest number that way.

(Also you’ll find most people aren’t going to bother importing a unitypackage to answer forum questions, better to post script text in code tags with screenshots for any non-code stuff.)

1 Like

Ideas? Examples?

I wanted to insert a package to better understand what I’m doing. In fact, I don’t usually attach packages! :slight_smile:

Thank for quick reply :slight_smile:

So basically, you have a number representing your rotation, and you’ll probably want to set the rotation in LateUpdate based on this number. (Note: do not attempt to read the current rotation back out as Euler angles, that way lies madness. This is why we’re not going to use the physics engine for this.) If this is a slot machine, it probably also has speed and some deceleration rate.

public float rotValue = 0f;
public float rotSpeed = 0f; //degrees per second
public float rotFriction = 1f; //we'll subtract this per second
public float roundToDegrees = 36f;
void LateUpdate() {
transform.rotation = Quaternion.AngleAxis(rotValue, Vector3.right);
}
void Update() {
rotValue += rotSpeed * Time.deltaTime;
rotSpeed -= rotFriction * Time.deltaTime;
rotSpeed = Mathf.Max(rotSpeed, 0f); //stop at 0

And now, when the speed reaches 0, we want it to find the nearest rounded value and then move to it:

if (rotSpeed <= 0f) {
float roundedRotValue = Mathf.Round(rotValue / roundToDegrees) * roundToDegrees;
rotValue = Mathf.MoveTowards(rotValue, roudnedRotValue, 60f * Time.deltaTime);
}
}

And that’s pretty much that. You can adjust the way it moves based on the desired feel of it but this should get you most of the way there.

1 Like

Many thanks for the way you gave me, no I thought of this solution! However the wheel is not a slot machine :slight_smile:

Thanks for the very valuable suggestion and your example!!