Scripting - Animations?

I can get animations from sketchfab etc. But as I know, I can’t script animations together :thinking: Im thinking of a firing anim and reload. If possible can you show me a script for scripting animations together like if you press R you do the reload animation?

Hi @Gabriel_Dobrzynski,

Check the following tutorial, it shows exactly that:

https://developer.playcanvas.com/en/tutorials/animation-blending/

1 Like

Hello @Leonidas
Would this code work?

var AnimationBlending = pc.createScript('animationBlending');

AnimationBlending.states = {
    reload: {
        animation: 'reload.json'
    }
};

// initialize code called once per entity
AnimationBlending.prototype.initialize = function() {
    this.blendTime = 0.2;


    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.keyDown, this);
    this.app.keyboard.on(pc.EVENT_KEYUP, this.keyUp, this);
};

AnimationBlending.prototype.setState = function (state) {
    var states = AnimationBlending.states;

    this.state = state;
    // Set the current animation, taking 0.2 seconds to blend from
    // the current animation state to the start of the target animation.
    this.entity.animation.play(states[state].animation, this.blendTime);
};

AnimationBlending.prototype.keyDown = function (e) {
    if ((e.key === pc.KEY_R) && (this.state !== 'reload')) {
        this.setState('reload');
    }
};

Because I have a problem

Where Animation Blending is (error)

Did you save the script after you pasted the code in it? Also did you parse animationBlending.js script before adding to the entity?

P.S. Try formatting the code you post here, especially when it’s huge, it helps in reading it.

1 Like

Well, I forgot to save it but now I can parse it but nothing comes up? Also, can’t reload yet :thinking:

Mmm, what do you expect to come up? The script doesn’t have any attribute, show in editor it won’t show anything apart from its name when added to a script component.

1 Like

Can I please show you my editor? https://playcanvas.com/editor/scene/1097311 :confused: Im really confused, I cannot reload, while in the animation blending I just press P and Im punching but I press R and Im not reloading?

The code works correctly, but stepping through with the debugger it seems the play() method on the animation component doesn’t fully execute. Because it seems the pistol entity is disabled?

1 Like

I enabled it? So I don’t get what the problem is

And, If I disable loop I can reload only once.

Sorry, not easy to debug in your project context, try sharing a simpler project that tests this code to check if it has an issue or not.

1 Like

@Leonidas
I have made a game with the gun and the animationBlending script (with the animation) but it still goes out the same :confused: https://playcanvas.com/editor/scene/1100466

Can you add me to it? leonidas

1 Like

So, I’ve moved forward and set Activate to false in the animation component.

Now if you try it the animation will correctly play if you press R, but only once.

This happens because when it finishes the current animation remains in the animation list, so subsequent calls to animation.play() don’t go through.

So a solution to this is to monitor when the animation finishes and reset the time and the current animation state. I’ve added the following method to your code:

AnimationBlending.prototype.update = function() {

    if( this.state === 'reload' && this.entity.animation.currentTime >= this.entity.animation.duration ){
        this.entity.animation.currentTime = 0;
        this.state = undefined;
    }
};
3 Likes

Thank you so much! This will be alot of help.

1 Like

Hey @Leonidas
I have alot of work today since my team are off.
So how to script a run animation?

this is fine this is wrong :thinking:

You should check the following example on how to run a more complex animation system:

https://developer.playcanvas.com/en/tutorials/third-person-controller/

3 Likes