Search Unity

Stop Animation in an exact rotation

Discussion in 'Scripting' started by MedalHellWay, Oct 15, 2019.

  1. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Hi All :)

    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!
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.)
     
    MedalHellWay likes this.
  3. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Ideas? Examples?

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

    Thank for quick reply :)
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
    Code (csharp):
    1. public float rotValue = 0f;
    2. public float rotSpeed = 0f; //degrees per second
    3. public float rotFriction = 1f; //we'll subtract this per second
    4. public float roundToDegrees = 36f;
    5. void LateUpdate() {
    6. transform.rotation = Quaternion.AngleAxis(rotValue, Vector3.right);
    7. }
    8. void Update() {
    9. rotValue += rotSpeed * Time.deltaTime;
    10. rotSpeed -= rotFriction * Time.deltaTime;
    11. rotSpeed = Mathf.Max(rotSpeed, 0f); //stop at 0
    12.  
    And now, when the speed reaches 0, we want it to find the nearest rounded value and then move to it:
    Code (csharp):
    1. if (rotSpeed <= 0f) {
    2. float roundedRotValue = Mathf.Round(rotValue / roundToDegrees) * roundToDegrees;
    3. rotValue = Mathf.MoveTowards(rotValue, roudnedRotValue, 60f * Time.deltaTime);
    4. }
    5. }
    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.
     
    MedalHellWay likes this.
  5. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Many thanks for the way you gave me, no I thought of this solution! However the wheel is not a slot machine :)

    Thanks for the very valuable suggestion and your example!!