Problems with my shooting script

var Shooting = pc.createScript(‘shooting’);

Shooting.attributes.add(‘raycastStartPoint’, { type : ‘entity’ });
Shooting.attributes.add(‘startPoint’, { type : ‘entity’ });
Shooting.attributes.add(‘endPoint’, { type : ‘entity’ });
Shooting.attributes.add(‘holeEntity’, { type : ‘entity’ });
Shooting.attributes.add(‘bulletEntity’, { type : ‘entity’ });
Shooting.attributes.add(‘armEntity’, { type: ‘entity’ });
Shooting.attributes.add(‘fireEntity’, { type: ‘entity’ });
Shooting.attributes.add(‘cameraEntity’, { type: ‘entity’ });

Shooting.attributes.add(‘ammoDisplay’, { type : ‘entity’ });

Shooting.prototype.initialize = function() {
this.ammoAmount = 20;
this.reloadTime = 0;

this.app.on('Player:Shoot', this.onShoot, this);
this.app.on('Player:Reload', this.onReload, this);
this.app.on('Player:UpdateAmmo', this.updateAmmo, this);
this.app.on('Player:UnlimitedAmmo', this.unlimitedAmmo, this);

};

Shooting.prototype.onShoot = function(ammo) {
if(this.isReload) return;
if(ammo <= 0) return;

var raycastStartPoint = this.raycastStartPoint.getPosition();
var from   = this.startPoint.getPosition();
var to     = this.endPoint.getPosition();

this.app.fire('Network:Shoot', from, to);

var num = Math.floor(Math.random() * 3);
var either1 = Math.floor(Math.random()*2 + 1);
var either2 = Math.floor(Math.random()*2 + 1);
var either3 = Math.floor(Math.random()*2 + 1);

if(either1 === 1) {
    this.randomX = Math.random() * num;
} else {
    this.randomX = Math.random() * -num;
}

if(either2 === 1) {
    this.randomY = Math.random() * num;
} else {
    this.randomY = Math.random() * -num;
}

if(either3 === 1) {
    this.randomZ = Math.random() * num;
} else {
    this.randomZ = Math.random() * -num;
}

this.toRan = new pc.Vec3(to.x + this.randomX, to.y + this.randomY, to.z + this.randomZ);

var result = this.app.systems.rigidbody.raycastFirst(raycastStartPoint, this.toRan);

    if(result){           
        setTimeout(function(self){
            var hole = self.holeEntity.clone();
                hole.setPosition(result.point);

            self.app.root.addChild(hole);

            setTimeout(function(){
                hole.destroy();
            }, 3000);
        }, 100, this);
        console.log(result);
    }

    var bullet = this.bulletEntity.clone();
        bullet.setPosition(from);
        bullet.lookAt(this.toRan);
        bullet.enabled = true;

    this.app.root.addChild(bullet);

    setTimeout(function() {
        bullet.destroy();
    }, 1000);
    
this.armEntity.setLocalPosition(0, 0, 0);
this.armEntity.tween(
    this.armEntity.getLocalPosition()
).to({ x : 0, y : 0, z : 0.050 }, 0.1, pc.BackOut).start();

};

Shooting.prototype.updateAmmo = function(ammo) {
this.ammoDisplay.element.text = ‘Ammo : ’ + ammo + ’ / 20’;
};

Shooting.prototype.onReload = function() {
if(this.ammoAmount >= 20) {
return;
} else if(this.ammoAmount < 20 && this.ammoAmount > -1) {
this.ammoAmount = 20;
}
};

Shooting.prototype.unlimitedAmmo = function() {
if(this.app.keyboard.wasPressed(pc.KEY_F)) {
this.ammoAmount = 9999;
this.app.fire(‘Player:UpdateAmmo’);
}
};
This is the script for my game but i cant shoot because there is errors and here is our editor https://playcanvas.com/editor/scene/1077347

1 Like

Hi @Gabriel_Dobrzynski,

Your attributes do not appear to be populated in the editor. Make sure you click “Parse” on that script, and populate the attributes.

1 Like