[SOLVED] How to disable animation script

How to disable animation script? Any tutorial to disable it?

Via code? You can get the script instance and set the enabled property to false.

https://developer.playcanvas.com/en/api/pc.ScriptType.html#enabled

var CollisionDetection = pc.createScript('collisionAlert');

CollisionDetection.attributes.add('animation', { type: 'boolean', default: true});

CollisionDetection.prototype.initialize = function() {
 this.entity.collision.on("collisionstart", this.onCollisionStart, this);
};

CollisionDetection.prototype.onCollisionStart = function(entity) {    
this.animation = this.app.root.findByName('Model').script.PlayerAnimation;

 if (entity.other.tags.has('player')) {
this.animation = false;
}
};

Is it like this?

Close! Guessing from your code, it should be:

var CollisionDetection = pc.createScript('collisionAlert');

CollisionDetection.attributes.add('animation', {
    type: 'boolean',
    default: true
});

CollisionDetection.prototype.initialize = function () {
    this.entity.collision.on("collisionstart", this.onCollisionStart, this);
};

CollisionDetection.prototype.onCollisionStart = function (entity) {
    this.animation = this.app.root.findByName('Model').script.playerAnimation;

    if (entity.other.tags.has('player')) {
        this.animation.enabled = false;
    }
};

i think it’s not working. i still can move it.

Can you post the project link please?

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

The issue here is disabling a script stops it’s updated being called.

In this case, the keyboard input is being done by events which are still listened to even when the script is disabled.

You can do one of two things here:

  1. Listen for the enabled event on the script to remove/add the keydown/keyup events
  2. Add this.enabled check in the keyChange function callback

Also, the playerAnimation script doesn’t handle movement, only animation.

previously I wanted to make it in PlayerMovement but the animation didn’t work

What do you want to happen when a collision happens?

So I wanted to make when I hit the finish line, the character would be like a celebration. but because the PlayerAnimation script can’t stop, the celebration will definitely stop because it will run another animation

Here’s a fixed version of your project. https://playcanvas.com/project/736776/overview/character-move-from-forums

I’ve made changes in collisionAlert.js and playerAnimation.js :slight_smile:

2 Likes

Thank you very much. it works really well