Fully Automatic Weapon

I have a shooting script, however i want an attribute that lets me toggle in editor whethor or not the weapon is fully automatic or not

var Shoot = pc.createScript('shoot');

Shoot.attributes.add('maxAmmo', {type : 'number', default : 30});
Shoot.attributes.add('range', {type : 'number', default : 30});
Shoot.attributes.add('text', {type : 'entity'});
Shoot.attributes.add('text2', {type : 'entity'});
Shoot.attributes.add('particleSys', {type : 'entity'});
Shoot.attributes.add('debugColor1', {type : 'rgba'});
Shoot.attributes.add('gunPoint', {type : 'entity'});
Shoot.attributes.add('hitImpulse', {type : 'number', default : 10});
Shoot.attributes.add('damage', {type : 'number', default : 10});
Shoot.attributes.add('fireRate', {type : 'number', default : 10});
Shoot.attributes.add('camera', {type : 'entity'});
Shoot.attributes.add('automatic', {type : 'boolean'});
Shoot.attributes.add('manual', {type : 'boolean'});







// initialize code called once per entity
Shoot.prototype.initialize = function() {
    this.currentAmmo = 0;
    this.currentAmmo = this.maxAmmo;
    this.canShoot = true;
    this.TimeSinceGameStarted = 0;
    this.rateOfRire = 0;    
};

// update code called every frame
Shoot.prototype.update = function(dt) {
    this.TimeSinceGameStarted += dt;
    var from = this.gunPoint.getPosition();
    var to = this.camera.camera.screenToWorld(screen.width / 2 , screen.height / 2 - 37, this.range);
    var result = this.app.systems.rigidbody.raycastFirst(from, to);  
    this.rateOfRire -= dt;
    
    
    if(this.currentAmmo === 0)
    {
        this.canShoot = false;
    }
    else
    {
        this.canShoot = true;
    }
    
    
    if(this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT) && this.canShoot === true && this.manual)
    {
        this.particleSys.particlesystem.reset();
        this.particleSys.particlesystem.play();
        this.particleSys.particlesystem.reset();
        
        if(result)
        {
            result.entity.collision.entity.rigidbody.applyImpulse(this.camera.forward.scale(this.hitImpulse), result.normal);
            this.currentAmmo -= 1;
            
        }else
        {
            return;
        }
        
        if(result.entity.name == 'fastboi zombie')
        {
            result.entity.script.target.TakeDamage(this.damage);
        }
        
        
        
    }
    
    if(this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT) && this.canShoot === true && this.automatic && this.TimeSinceGameStarted >= this.rateOfRire)
    {
        this.particleSys.particlesystem.play();
        this.particleSys.particlesystem.reset();
        this.currentAmmo -= 1;
        this.rateOfRire = this.TimeSinceGameStarted += 100 + 10 / this.fireRate;
        
        if(result)
        {
            result.entity.collision.entity.rigidbody.applyImpulse(this.camera.forward.scale(this.hitImpulse), result.normal);
            
            
        }else
        {
            return;
        }
        
        if(result.entity.name == 'fastboi zombie')
        {
            result.entity.script.target.TakeDamage(this.damage);
        }
        
        
        this.rateOfRire = this.fireRate;
        
    }
    
    if(this.app.keyboard.wasPressed(pc.KEY_R))
    {
        
        this.currentAmmo = this.currentAmmo + (this.maxAmmo - this.currentAmmo);
        
         
    }
    
    this.text.element.text = this.currentAmmo;
    this.text2.element.text = '/ ' + this.maxAmmo;
    
};


Is line 14/15 of your script above not what you are looking for?

sorry about that albertos, didn’t even notice the attribute setting lolll, i got this shooting script from one of my old projects so i didn’t remember programming fully automatic functionality

1 Like

okay the script does have major issues, when i switch the weapon to an automatic weapon it does not use any of the values i put it, it shoots way too fast no matter how i change the values of the attribute

What is this.rateOfRire?

I’m not sure what you expect of this.TimeSinceGameStarted >= this.rateOfRire in your if statement.

Also this.rateOfRire = this.TimeSinceGameStarted += 100 + 10 / this.fireRate; doesn’t look correct to me, so I would check the result with console.log(this.rateOfRire);.

this is an old script that my friend made, so i dont know if its outdated or just not working, i barely know programming the way it is, is there a way where i can fix this to actually use the attribute values and be fully automatic?

I think the script you shared is probably working with some fixes, but for this you need to debug the script to see what’s working and what’s not working. Do you have simple sample project so someone can check this for you?

Below I wrote a small example script that execute the shoot function with a small delay as long as the player is shooting. Please note that I didn’t test the script.

var Shoot = pc.createScript('shoot');

// can be set to true in another script
var shooting = false;

// initialize code called once per entity
Shoot.prototype.initialize = function() {
     this.timer = 0;
     this.delay = 0.2;
};

// update code called every frame
Shoot.prototype.update = function(dt) {
    if (shooting) {
        this.timer += dt;

        if (this.timer > this.delay) {
            this.shoot();
            this.timer = 0;
        }
    }
};

// shoot function
Shoot.prototype.shoot = function() {
    // fire your bullet here
};

how do i debug the script lol
im not very good at coding tbh

You can add logs in your script and use the console of your browser to see if they are executed in the expected order or report the expected result. (You can open the console of your browser with the F12 key).

console.log('Check 1'); // handy to see if executed at the expected moment
var value = 12;
console.log(value); // handy to see if something returns the expected result