Is there a way to make my weapon single fire?

Currently, i am trying to make my script single-fire (because it’s a sniper rifle)
editor = PlayCanvas | HTML5 Game Engine

var GunShoot = pc.createScript('gun');

GunShoot.prototype.update = function(dt) {
       
       var app = this.app;
this.sniper = this.app.root.findByName('sniper');    
   if (this.app.root.findByName('sniper').enabled === true) {
       // shooting code 
        if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
            // shoot
            if (this.magsizeP !== 0) {
                var bullet = this.app.root.findByName('bulletP').clone();
                this.app.root.addChild(bullet);
                bullet.reparent(this.app.root);
                bullet.setPosition(this.app.root.findByName('bulletP').getPosition());
                bullet.setRotation(this.app.root.findByName('bulletP').getRotation());
                bullet.enabled = true;
                console.log(bullet);
                // update magazine
            }
        
}
}
};

Hi @Thebosser.24,

The easiest way to approach this would be creating a variable that checks if the weapon has fired and only fire if it hasn’t. You could do something like:

var GunShoot = pc.createScript('gun');

GunShoot.prototype.initialize = function() {
  this.sniperHasFired = false;
}

GunShoot.prototype.update = function(dt) {
       
       var app = this.app;
this.sniper = this.app.root.findByName('sniper');    
   if (this.app.root.findByName('sniper').enabled === true) {
       // shooting code 
        if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT) && !this.sniperHasFired) {
            // shoot
            if (this.magsizeP !== 0) {
                var bullet = this.app.root.findByName('bulletP').clone();
                this.app.root.addChild(bullet);
                bullet.reparent(this.app.root);
                bullet.setPosition(this.app.root.findByName('bulletP').getPosition());
                bullet.setRotation(this.app.root.findByName('bulletP').getRotation());
                bullet.enabled = true;
                this.sniperHasFired = true;
                console.log(bullet);
                // update magazine
            }
        
        }
        else {
            this.sniperHasFired = false;
        }
    }
};

This isn’t a super clean way of doing it, but it should get you going in the right direction. Notice how the app marks this.sniperHasFired as false once the mouse is released.

I hope this is helpful

1 Like

To include a single fire on this script, you can add a timer that prevents the player from shooting too rapidly. Here is an example of how you could modify the script:

var GunShoot = pc.createScript('gun');

GunShoot.prototype.initialize = function() {
    this.canShoot = true;
    this.shootDelay = 0.5; // adjust as needed
};

GunShoot.prototype.update = function(dt) {
    var app = this.app;
    this.sniper = this.app.root.findByName('sniper');    
    if (this.app.root.findByName('sniper').enabled === true) {
        // shooting code 
        if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT) && this.canShoot) {
            // shoot
            if (this.magsizeP !== 0) {
                var bullet = this.app.root.findByName('bulletP').clone();
                this.app.root.addChild(bullet);
                bullet.reparent(this.app.root);
                bullet.setPosition(this.app.root.findByName('bulletP').getPosition());
                bullet.setRotation(this.app.root.findByName('bulletP').getRotation());
                bullet.enabled = true;
                console.log(bullet);
                
                // update magazine
                
                this.canShoot = false;
                setTimeout(() => {
                    this.canShoot = true;
                }, this.shootDelay * 1000); // convert to milliseconds
            }
        }
    }
};

This modification introduces two new variables: canShoot and shootDelay. canShoot is a boolean that tracks whether the player is allowed to shoot again. shootDelay is the amount of time (in seconds) that must pass before the player can shoot again. The setTimeout function is used to reset canShoot after the appropriate delay.

With this modification, the player can shoot once every shootDelay seconds. You can adjust the shootDelay value to control the rate of fire.

2 Likes