I’m trying to add a bullet counter system to my FPS game project. This is my script:
var BulletCounter = pc.createScript('bulletCounter');
BulletCounter.attributes.add('maxAmmo', { type: 'number', default: 10 });
BulletCounter.attributes.add('magazineSize', { type: 'number', default: 30 });
BulletCounter.attributes.add('bullet', { type: 'entity' });
BulletCounter.attributes.add('gunpower', { type: 'number', default: 1 });
BulletCounter.attributes.add('bulletCount', { type: 'entity' });
BulletCounter.attributes.add('currentAmmo', { type: 'number' });
// initialize code called once per entity
BulletCounter.prototype.initialize = function () {
this.currentAmmo = this.maxAmmo;
var app = this.app;
app.mouse.on("mousemove", this._onMouseMove, this);
app.mouse.on("mousedown", function () {
app.mouse.enablePointerLock();
if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
this.shoot();
}
}, this);
};
// update code called every frame
BulletCounter.prototype.update = function (dt) {
this.bulletCount.element.text = this.entity.this.currentAmmo + '/' + this.entity.this.magazineSize;
console.log('this works');
};
BulletCounter.prototype.shoot = function (dt) {
this.currentAmmo--;
// Clone the bullet as specified by the attribute
var bullet = this.bullet.clone();
// Add it to the game
this.app.root.addChild(bullet);
var player = this.entity;
var target = this.app.root.findByName('target');
// Its force is in the direction the player is facing
this.force = new pc.Vec3();
this.force.copy(target.forward);
this.force.scale(this.gunpower);
var pos = target.getPosition();
var direction = target.forward;
// Add it a little further if the player is already going fast
// pos.add(direction.scale(5 + gun.rigidbody.linearVelocity.length() * 0.01));
bullet.setPosition(pos);
// var bulletRotation = player.getRotation();
// bullet.setRotation( bulletRotation );
// bullet.rotateLocal(90,0,0);
bullet.enabled = true; //Must enable after setting position!
bullet.rigidbody.applyImpulse(this.force);
};
};
I added a text element entity in the hierarchy and referencing the text in my script. I believe this line: “This.bulletCount.element.text = this.entity.this.currentAmmo + ‘/’ + this.entity.this.magazineSize;” should show the current number of bullets and the magazine size on the screen and everytime i shoot, the current number of bullets should reduce by one. I added a console.log under that line but i get no message in the console when i launch the game. I think something is wrong with that line(or another part of the script)but can’t find out what. I would appreaciate any kind of assistance for my problem