Animations not playing multiple times (not using loop)

Hello, all.
I have two objects in my game that have virtually the same issue.

The lighter in my game, when selected for the first time, has an opening animation, but when selected a second time, doesn’t play the animation again.

Simmilarly, the gun in my game has a reloading over animation that plays if R is pressed and you are out of loaded ammo. It only works once, and I cannot reload the gun after the first time.

Here is the code I use for the lighter:

var Lighter = pc.createScript('lighter');
Lighter.attributes.add('flame',{type:'entity'})
// initialize code called once per entity
Lighter.prototype.initialize = function() {

};

// update code called every frame
Lighter.prototype.update = function(dt) {
if(this.entity.enabled === true){
    this.entity.anim.setBoolean('open', true);
    this.flame.enabled = true;
}else{
        this.entity.anim.setBoolean('open', false);
          this.flame.enabled = false;
};

Here are some pictures of the anime state graph for the lighter:



Here is the code for the gun:
(Reload is handled on lines 20-23 and 66-71)

var Gun = pc.createScript('gun');
Gun.attributes.add('bullet', { type: "entity" });
Gun.attributes.add('mag', { type: "number" });
Gun.attributes.add('cooldown', { type: "number" });
Gun.attributes.add('bulletTxt', { type: "entity" });
Gun.attributes.add('reloadAnimTime', { type: "number" });




// initialize code called once per entity
Gun.prototype.initialize = function () {

    this.rounds = this.mag;
    this.isShooting = false;

    this.timer = 0;
    this.isTimerActive = true;

    this.entity.anim.on('reloadAnimDone', function () {
        this.rounds = this.mag;
        //  alert('reload Sucessful');
    }, this);
};

// update code called every frame
Gun.prototype.update = function (dt) {
    this.bulletTxt.element.text = this.rounds;
    // update function
    if (this.bulletTxt.element.opacity > 0) {
        this.bulletTxt.element.opacity -= 0.1;
        this.bulletTxt.element.outlineThickness -= 0.1;
    };

    if (!this.isTimerActive)
        return;

    this.timer += dt;

    if (this.app.keyboard.isPressed(pc.KEY_Q) && this.rounds != 0 && this.timer > this.cooldown) {
        this.playsound();
        this.isShooting = true
        this.timer = 0;
        this.rounds = this.rounds - 1;
        this.bulletTxt.element.opacity = 1;
        this.bulletTxt.element.outlineThickness = 1;
        var newBullet = this.bullet.clone();
        this.app.root.addChild(newBullet);
        newBullet.setPosition(this.bullet.getPosition());
        newBullet.enabled = true;
        //alert(this.rounds);
        this.timer = 0;
    } else {
        this.isShooting = false;
    };

    //if (this.isShooting === true) {
    //  if (!this.entity.sound.isPlaying('gunShot')) {
    //            this.entity.sound.play('gunShot');
    //    };
    // }
    // else {
    //   this.entity.sound.stop('gunShot');
    //};

    if (this.app.keyboard.isPressed(pc.KEY_R) && this.rounds === 0) {
        this.entity.anim.setBoolean('reload', true);
    } else {
        this.entity.anim.setBoolean('reload', false);

    };
};

Gun.prototype.playsound = function () {
    if (!this.entity.sound.isPlaying('gunShot')) {
        this.entity.sound.play('gunShot');

    };
};

// uncomment the swap method to enable hot-reloading for this script
// update the method body to copy state from the old instance
// Gun.prototype.swap = function(old) { };

// learn more about scripting here:
// https://developer.playcanvas.com/user-manual/scripting/

Here are some pictures of the gun’s anim state graph:


Here is the editor link:PlayCanvas | HTML5 Game Engine
To find the two entities I am referring to:
Dev Test Controller
-View
-Hand models

Under hand models there is the lighter and R1 (the gun)

I am grateful for any help. If there is anything else I can provide please let me know!

1 Like

Hi @Codeknight999!

I don’t see anything wrong, but the default states of the graph are empty and don’t have an animation attached. I’m not sure if this could cause the problem.

I suggest to create a graph where you also use the default state. You can rename the state if you want.

I was wondering if that was the problem. I’ll
Make some animations that do nothing tomorrow and get back to you.

1 Like

Is there a reason you cannot use one of the two states as the default state as well? As soon this animation is finished it will stay as it is, or am I incorrect?

For the lighter; when lighter played the opening animation then played an animation where it was open, it should be closed and ready to open when disabled.

This is kinda unrelated, but is there a way to use json files for animations? I use blockbench and the only way to get new animations is to upload a GLB and copy the animations.

I don’t know, but the topic below is related.

Tried this for the gun and it didn’t work. I currently don’t have the lighter model on hand to test it.

I did a little debugging and I figured out that the issue is solely the animations, it reads the rounds fine.

I just saw that you have set the exit time to 4.8. That value will probably never be reached, because it is a normalized value. That means 1 is the full animation. (There is an exception here when you change the speed of the animation).

If you set the exit time to 1, the problem is solved.

OHHHH.
I didnt know if it was a percentage or timing. Let me change that and test it

It works now, thank you so much!!!

I just realized it doesn’t work on the lighter. I updated the exit time to 1.