Animation blend, every keypress changes animation

Firstly, how do i post code in here so it shows up in a box?

The animation transition and blending is working, but now for some reason every key makes the walking animation happen, here is the code.

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

AnimationBlending.states = {
    idle: {
        animation: 'idle'
    },
    walking: {
        animation: 'walking'
    }
};

// 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 || pc.KEY_A || pc.KEY_S || pc.KEY_D) && (this.state !== 'walking')) {
        this.setState('walking');
    }
};

AnimationBlending.prototype.keyUp = function (e) {
    if ((e.key === pc.KEY_W || pc.KEY_A || pc.KEY_S || pc.KEY_D) && (this.state === 'walking')) {
        this.setState('idle');
    }
};

Hi @Aaron_B!
This is happening because your animation blending or playerAnimationHandler is just making it walk foward, left, right and back in the same animation.

Look, it says

this.setState('walking'); 

You have set it to W A S D. (So every direction you go in it goes in the walking animation)

@Gabriel_Dobrzynski I shoulda been more specific my apologies. Im good with how it works with each of those keys. Every OTHER key makes the walking animation play. like P, T, K etc…

1 Like

Hello @Aaron_B!

Probably the reason is your if statements. Try to do the same for every key as you do for the first key in your if statements.

2 Likes

You need e.key === in each part

    (e.key === pc.KEY_W || e.key === pc.KEY_A || e.key === pc.KEY_S || e.key === pc.KEY_D)

EDIT: @Albertos already answered

2 Likes

Thank you to all of you for helping! SOLVED

1 Like