'How to stop animation on the start position / frame zero

I tried to stop the animation objects with a button, I want to when it stops, the object is located at the starting position. For example I have a cube with animated moving position. when the stop button is pressed, the cube back to its original position.



Solution 1:[1]

When no animations are playing, Unity does not change the object's position at all - so stopping it will not reset the position for you. It will just not move it any more.

If you want to return the object to the configuration it was in before you started playing your animation, you have to do it yourself. Your options are:

  • Make the start position be set by an animation as well. That way you can just play that animation after the other one has finished.
  • Make the animation you're playing return the object to its start position at the end - i.e. have the last frame of the animation be back at the object's start position.
  • Write code that captures and restores the object's position, by saving the values of transform.position/transform.rotation and then setting them again when the animation has finished.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1