Play animation in reverse

I have this code:

                  this.app.on('ButtonClicked:SportBegin', function(){
   // add custom logic
        console.log("button clicked");       
          this.app.root.findByName("AnimationCamera").animation.play("1:START-ENTRENCE|15:LOUNGE-GYM1.glb", 0.8);
        
 
}, this);

So it plays an animation when I click on a button called: “SportBegin”. Now how can I play it in reverse? I read on the forum that you can set the anim.speed to -1 to play in reverse but how do I add that to this code?

Best,
Maarten

Hi @Maarten_deVries,

In your case I think to set the animation component speed you can do it like this:

var animationComponent = this.app.root.findByName("AnimationCamera").animation;

animationComponent.speed = -1;
animationComponent.play("1:START-ENTRENCE|15:LOUNGE-GYM1.glb", 0.8);
1 Like

this.entity.animation.speed=-1;

Just get the entity and apply the top command.

1 Like

Thank you.

I have added this to my code and now I get this error:

Any idea how to fix this?

You need to add the code inside the event handler I think, not sure what you are trying to do by commenting out like that.

In any case, if you want to do that, see line 22 where you commented out that event handler? You need to comment out line 33 as well, it can’t live on its own like that, hence the error.

Can you share that file?

If I comment out line 33 the animation directly starts playing when I launch the project.

https://playcanvas.com/editor/scene/1149789

You need to add the code inside your button event handler, much like you had it before. Just add in addition before the play() method the speed set property.

1 Like

File Name?

PlayAnimation.js

As


Mentioned.
The code is outside your event handler. You commented half of the event handler code.

1 Like

For this just stop calling that event handler at the start.

Is It fixed?

Thank you guys it is fixed now!

2 Likes