rive-app / rive-unity

MIT License
104 stars 11 forks source link

How to reset a Rive animation? #51

Open tomkail opened 6 months ago

tomkail commented 6 months ago

Hi! The docs don't seem to show a way to reset a Rive animation to the default state (including state machines etc). What is the correct way to do this?

abethke commented 5 months ago

Have you tried turning it off and on again? Disable and then re-enable the related GameObject? It's hacky but it should work

tomkail commented 5 months ago

Haha good tech support. This is a custom component implementing the Rive libraries. We can just load the asset again but I had assumed there’d be an easier way?

On Tue, 4 Jun 2024 at 15:32, Alex Bethke @.***> wrote:

Have you tried turning it off and on again? Disable and then re-enable the related GameObject? It's hacky but it should work

— Reply to this email directly, view it on GitHub https://github.com/rive-app/rive-unity/issues/51#issuecomment-2147692620, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJR3UFQNKZ3QHN2X6NZVRDZFXFXTAVCNFSM6AAAAABHT63JB6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBXGY4TENRSGA . You are receiving this because you authored the thread.Message ID: @.***>

abethke commented 5 months ago

I'm just another dev killing time while waiting for anyone to respond to my own ticket ;)

In my usecase the Rive animation just loops infinitely so I've not looked for a more elegant way to restart it.

damzobridge commented 5 months ago

Hi Tom,

Could you give me a bit more detail about what you're trying to achieve? If you're looking to adjust the playback speed of your animations, you can use the advance() method on your State Machine instance. For instance, to double the speed, you can multiply elapsedSeconds by 2, like so: stateMachine.advance(elapsedSeconds * 2);.

However, since you're more likely referring to resetting your state machine back to its initial state. The recommended way to do this is to coordinate this within your rive file, and control the different changes to the state machine in Unity using inputs.

For example, in your rive file (in the rive editor), you could add a trigger input such as onReset and make it so that when that is fired, the state machine returns to its initial state.

You can then fire this trigger in unity and the animation will reset.

Working with rive files is a bit different from working with plain animations in that Rive files don't just hold animations, they also contain behaviours. A lot of the things you would usually manually coordinate in Unity in terms of timing animations, for example, can be handled within the Rive file itself, so all you need to worry about in Unity is changing inputs and responding to events. We're working on a lot more examples that illustrate this a bit more.

tomkail commented 5 months ago

I simply want to reset the rive player, as if it had just started. We can unload it/load it but that seems heavy, and setting up a reset state on the animation seems like extra work. If there’s no other means then we’ll just unload/reload.

On Thu, 6 Jun 2024 at 22:19, Adam @.***> wrote:

Hi Tom,

Could you give me a bit more detail about what you're trying to achieve? If you're looking to adjust the playback speed of your animations, you can use the advance() method on your State Machine instance. For instance, to double the speed, you can multiply elapsedSeconds by 2, like so: stateMachine.advance(elapsedSeconds * 2);.

However, since you're more likely referring to resetting your state machine back to its initial state. The recommended way to do this is to coordinate this within your rive file, and control the different changes to the state machine in Unity using inputs.

For example, in your rive file (in the rive editor), you could add a trigger input such as onReset and make it so that when that is fired, the state machine returns to its initial state.

You can then fire this trigger in unity https://rive.app/community/doc/state-machines/docHnjaSeIIr and the animation will reset.

Working with rive files is a bit different from working with plain animations in that Rive files don't just hold animations, they also contain behaviours. A lot of the things you would usually manually coordinate in Unity in terms of timing animations, for example, can be handled within the Rive file itself, so all you need to worry about in Unity is changing inputs and responding to events. We're working on a lot more examples that illustrate this a bit more.

— Reply to this email directly, view it on GitHub https://github.com/rive-app/rive-unity/issues/51#issuecomment-2153424992, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJR3UAATJUE6A5W2QR3S7LZGDG4LAVCNFSM6AAAAABHT63JB6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNJTGQZDIOJZGI . You are receiving this because you authored the thread.Message ID: @.***>

damzobridge commented 5 months ago

Ohh understood. Looks like this is available in some of our other runtime packages, but not currently in the Unity package.

We'll look into getting this added! Thanks for bringing it up, in the meantime, the unloading/loading approach you've mentioned is the alternative workaround if updating the rive file to add a reset trigger isn't an option.