[SOLVED] Delta time isn't going up

While I was trying to make a cooldown for the gun it can’t fire after 0.2 seconds because dt is not going up. Please help?

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

GunShoot.attributes.add('player', { type: 'entity' });
GunShoot.attributes.add('camera', {type: 'entity', description: 'Optional, assign a camera entity, otherise one is created'});
GunShoot.attributes.add('bulletFx', { type: 'entity' , description: 'Bullet effect system' });
GunShoot.attributes.add('qPress', { type: 'boolean' , defalut: false, description: 'q or e activates weapon'});
GunShoot.attributes.add('fPress', { type: 'boolean' , defalut: false, description: 'f to destroy bullet'});
GunShoot.attributes.add('automatic', { type: 'boolean' , defalut: false, description: 'automatic or manual shoot'});
GunShoot.attributes.add('impactForce', { type: 'number' , default: 10, description: 'Impact force' });
GunShoot.attributes.add('playerimpactForce', { type: 'number' , default: 5000, description: 'Player Impact force' });
GunShoot.attributes.add('millisecTillDestroy', { type: 'number' , default: 1000, description: 'milliseconds until the bullet is destroyed' });
millisecTillDestroy = this.millisecTillDestroy;
qPress = this.qPress;
fPress = this.fPress;
automatic = this.automatic;

// initialize code called once per entity
GunShoot.prototype.initialize = function() {
    // Event for mouse button left fire
    this.app.keyboard.on(pc.KEY_DOWN, this.KEY_DOWN, this);

};
// update code called every frame
GunShoot.prototype.update = function(dt) {
    this.cooldown =+ dt
    //check if automatic shoot is true or false
    if (this.automatic == true){
        // Check for if Q or E to fire weapon
        if(this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT) && this.cooldown >= 0.2) {
        // Process action
            this.shoot();            
        } 
    }
    if (this.automatic == false){
        // Check for if Q or E to fire weapon
        if(this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT)) {
        // Process action
            this.shoot();            
        } 
    }

    };
// handle shooting and raycast
GunShoot.prototype.shoot = function(e) {
    // Get center of screen
    var centerScreenX = this.app.graphicsDevice.width / 2;
    var centerScreenY = this.app.graphicsDevice.height / 2;
    //Get start and end point
    var start = this.camera.camera.screenToWorld(centerScreenX, centerScreenY, this.camera.camera.nearClip);
    var end = this.camera.camera.screenToWorld(centerScreenX, centerScreenY, this.camera.camera.farClip); 
    // raycast between the two points and return the closest hit result
    var result = this.app.systems.rigidbody.raycastFirst(start, end);
    if(this.entity.sound != null) {
        // Play this weapons sound
        this.entity.sound.play("fire");
        console.log('sound played');
    }
    // We hit something with collision
    if(result) {
        console.log("You Hit: " + result.entity.name);
        this.handleImpact(result.entity,result.point,result.normal);
        // Fire a hit event on object
        result.entity.fire("hitTarget",this.player);
        if (result.entity.tags.has('breakable')){
            console.log("You Hit brick btw");
            result.entity.destroy();
        }
 
    } else {
        console.log("Nothing Hit");
    }
    this.cooldown = 0
};

// handle impact
GunShoot.prototype.handleImpact = function(rsltEntity, rsltPoint, rsltNormal) {
    var bullet = this.bulletFx.clone();
    this.app.root.addChild(bullet);
    bullet.setPosition(rsltPoint);
    // Enable the bullet
    bullet.enabled = false;
    bullet.enabled = true;
    // Fire bullet system
    bullet.fire('impact');
    // Check if what I hit has rigid body
    if(rsltEntity.rigidbody != null) {
        this.force = new pc.Vec3();
        this.force.copy(rsltNormal);
        this.force.scale(-this.impactForce);
        rsltEntity.rigidbody.applyImpulse(this.force);
    }
    if(rsltEntity.tags.has('boost')) {
        playerpos = this.entity.getPosition();
        boostpos = rsltEntity.getPosition();
        distance = Math.hypot(
            playerpos.x - boostpos.x,
            playerpos.z - boostpos.z
            );
        console.log(distance);
        if (distance < 5){
        this.force = new pc.Vec3();
        this.force.copy(rsltNormal);
        this.force.scale(this.playerimpactForce);
        this.entity.rigidbody.applyImpulse(this.force);
        }

    }
    if (this.fPress == false){
        setTimeout(function(){ // There is a better way for timeout
            bullet.destroy();
        },this.millisecTillDestroy);
    }
    if (this.fPress == true){
        if (this.app.keyboard.isPressed(pc.KEY_F)){
        setTimeout(function(){
            bullet.destroy();
        },this.millisecTillDestroy);
        }
    }
};

Hi @Literally_Kirby!

On line 25 of the code above you should use += instead of =+.

I’ve added this, still doesn’t work

I fixed it I just had to define cool down as a int in the int

1 Like