Include jump in script

i have te walk animation and the idle animation, and i want to include the jump animation, but i dont know how to inlcude

project: https://launch.playcanvas.com/862739?debug=true


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

AnimationBlending.states = {
    idle: {
        animation: 'Idle.json'
    },
    Jump: {
        animation: 'Jump.json'
    },
    Walk: {
        animation: 'Walk.json'
    }
};

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

    this.setState('idle');

    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_W) && (this.state !== 'Walk')) {
        this.setState('Walk');
    }
};

AnimationBlending.prototype.keyUp = function (e) {
    if ((e.key === pc.KEY_W) && (this.state === 'Walk')) {
        this.setState('idle');
    }
};

Hi @Trap_Spirit,

From what I see in your code you pasted above you can do something similar in the keyUp method:

AnimationBlending.prototype.keyUp = function (e) {

    if ((e.key === pc.KEY_W) && (this.state === ‘Walk’)) {
        this.setState(‘idle’);
    }

    // e.g. jumping works with space key
    if (e.key === pc.KEY_SPACE) {
        this.setState(‘jump’);
    }
};
1 Like