Conditional Animations Part 2

Hello everyone,

I’ve been having issues with getting animations to start and stop based on condition (i.e. true or false). I posted an example PG yesterday, and @sebavan solved the issue. However, I realized that yesterday’s example didn’t really replicate the issue I’m having in my project closely enough.

So…

Here’s a new PG, and a new problem to solve:

If the rabbit hasEnergy = true, then the jumping animation should start.

When it runs out of energy and hasEnergy = false, the falling animation should start.

https://playground.babylonjs.com/#QY1WYT#252

I spent an entire day trying every combination of code I could think of to get this working :sob:. Being new to programming, I’m sure there is something simple I’m overlooking…

Thanks in advance for any help or suggestions! :smiley: :+1:

Afraid i don’t have time to make an example,
But look into get / set functions :slight_smile:
I’m sure this will solve your problem.
On set function,
if boolean is true, start animation
If false, stop animation

Hello @aWeirdo, and thanks for the suggestion.

Would using get / set functions be different from what I have in the PG?

   if (rabbitClone.metadata.hasEnergy == true){

      scene.beginAnimation(rabbitClone.skeleton, 0, 72, true, 1);

      };

In my project, this approach works for everything except for starting and stopping animations. :thinking:
For instance, in the PG below, the rabbits spins when the boolean is true, then falls when it switches to false.

https://playground.babylonjs.com/#QY1WYT#254

Hi @Anderson34 slightly different, yes :slight_smile:
If you want to use mesh.metadata,
i would use a class and do something like this;
https://playground.babylonjs.com/#QY1WYT#255
(secure scope = save scope), poor wording :smile:

I tried commenting and explaining different things, i hope it’s all understandable.
otherwise just ask.

as a side-note, i would use this approach for spinning etc too. create a basic float animation on the mesh rotation.y axis.
https://doc.babylonjs.com/babylon101/animations

then you can calculate and add exactly how much it should spin/rotate and how long time it should take to spin/rotate that predefined amount, giving you a lot more consistency :slight_smile:

3 Likes

Hello @aWeirdo,

Thanks for taking the time to make an example PG, and for the detailed explanations, I really appreciate it! I haven’t worked with classes and get / set functions yet, so this will be a good way for me to learn these elements of JavaScript.

Thanks again! :smiley: :+1:

1 Like