How would i implement weapon Firerate

this is my shooting script

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

GunShoot.prototype.update = function(dt) {
       
       var app = this.app;
this.pistol = this.app.root.findByName('pistol');
   if(this.app.keyboard.wasPressed(pc.KEY_R)) {
    this.magsizeP = 12;     
   }
   if (this.app.root.findByName('pistol').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
                this.magsizeP = this.magsizeP - 1;
            }
        }
   }
};

and can i add recoil?

Hi @Thebosser_24,

I don’t have an example in mind, but for fire rate you usually implement a cooldown timer. After each bullet fired you start a timer e.g. for 0.1 sec and in that period you don’t allow any more bullets to fire.

Do a forum search on how to add a timer in a PlayCanvas script.

For recoil you will need to move/animate your weapon between two threshold values. You can use PlayCanvas tween or lerping. It can be a bit advanced but do search on how to animate entities in the forum to get some good examples and get started.

Hope that helps.

3 Likes

2 posts were split to a new topic: How to add FPS camera recoil

hello? its been a week. and its nearly been 2 weeks since I made this.

Hi … have you tried the suggestions from @Leonidas ? They seemed very sensible to me.

1 Like