[SOLVED] Sprite Animation Not Switching

So, basically, I’m working on a Sonic recreation fangame. I simply want to be able to switch between an idle and walk animation. I tried making it so that if you’re velocity is 0, idle animation plays. Else, if the velocity is greater than or less than 0, walk animation plays. So I implemented my code, but it only runs idle. Walk doesn’t work. Here’s my code for the animation section:

PlayerController.prototype.animation = function() {
    if (this.entity.rigidbody.linearVelocity.x === 0) {
        this.entity.sprite.clip('Idle').play = true;
        this.entity.sprite.clip('Walk').play = false;
    }
    else if (this.entity.rigidbody.linearVelocity.x > 0 || this.entity.rigidbody.linearVelocity < 0) {
        this.entity.sprite.clip('Idle').play = false;
        this.entity.sprite.clip('Walk').play = true;
    }
};

This code is located in the PlayerController script. Does anyone know how to fix the problem? Did I make a mistake?

Link To Editor: https://playcanvas.com/editor/scene/898825

Did you already tried to add a console.log?

Try to check the velocity with

console.log(this.entity.rigidbody.linearVelocity.x);

inside the update.

1 Like

Hello, thank you for the reply. Let me try this.

I also see a mistake.

I have fixed it below.

PlayerController.prototype.animation = function() {
    if (this.entity.rigidbody.linearVelocity.x === 0) {
        this.entity.sprite.clip('Idle').play = true;
        this.entity.sprite.clip('Walk').play = false;
    }
    else if (this.entity.rigidbody.linearVelocity.x > 0 || this.entity.rigidbody.linearVelocity.x < 0) {
        this.entity.sprite.clip('Idle').play = false;
        this.entity.sprite.clip('Walk').play = true;
    }
};

Thank you. Also, in the console, it shows the velocity changed, but the sprite clips do not.

So do you know what the problem is?

Do you sure that

this.entity.sprite.clip().play

is a bool and not a function?

I’m not sure. I also tried using a different method, but it didn’t work either.

If you add me to the project i can try to help.

Ok i added you