i want it to say Reload! when it runs out of ammo. and if y’all’s want the script i can give
Hi @Thebosser.24,
In general, the way that I would approach this is checking the ammo count whenever you subtract from it, and if it is equal to 0 activating the text entity on the screen that you want to appear. You could do this by creating an entity attribute in your script that tracks ammo and then referencing it later in the script. It might look something like this:
...
RandomGunScript.attributes.add('reloadText', {
type: 'entity',
title: 'Reload Message'
});
RandomGunScript.prototype.initialize = function() {
this.ammo = 100;
};
RandomGunScript.prototype.firingFunction = function() {
if(this.ammo > 0) {
this.ammo--;
}
else {
this.reloadText.enabled = true;
}
};
RandomGunScript.prototype.reloadingFunction = function() {
this.ammo = 100;
this.reloadText.enabled = false;
};
...
Of course this is just meant as an example, but you should get a decent idea of how you might want to edit your existing scripts.
I hope this is helpful.
1 Like
this is helpful! thank you so much!
what does this do
RandomGunScript.prototype.reloadingFunction = function() { this.ammo = 100; this.reloadText.enabled = false; };
Can you help me what i did wrong
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('muzzleFx', { type: 'entity', description: 'Muzzle flash particle system' });
GunShoot.attributes.add('bulletFx', { type: 'entity' , description: 'Bullet effect system' });
GunShoot.attributes.add('impactForce', { type: 'number' , default: 100, description: 'Impact force' });
GunShoot.attributes.add('magSize', { type: 'number' , default: 15, description: 'Size of the weapons magazine'});
GunShoot.attributes.add('bulletCntr', { type: 'number' , default: 0, description: 'Current bullet count'});
GunShoot.attributes.add('Automatic', { type: 'boolean' , default: false, description: 'Fully automatic or semi-automatic'});
GunShoot.attributes.add('reloadText', {type: 'entity', title: 'Reload Message'});
// initialize code called once per entity
GunShoot.prototype.initialize = function() {
// Find the text element to use for update
this.ammoText = this.app.root.findByName("ammoCnt");
// Event for mouse button left fire
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.mouseDown, this);
};
// update code called every frame
GunShoot.prototype.update = function(dt) {
// Handle reload
if(this.app.keyboard.wasPressed(pc.KEY_R)) {
console.log("Reload Hit");
// Reload Magazine
this.bulletCntr = this.magSize;
}
// Update Ammo text
this.ammoText.element.text = this.bulletCntr + '/' + this.magSize;
};
GunShoot.prototype.firingFunction = function() {
if(this.bulletCntr > 0) {
this.bulletCntr--;
}
else {
this.reloadText.enabled = true;
}
};
GunShoot.prototype.reloadingFunction = function() {
this.bulletCntr = 100;
this.reloadText.enabled = false;
};
// Handle mouse down event
GunShoot.prototype.mouseDown = function(e) {
// Check for Mouse left button
if(this.entity.enabled && e.button == pc.MOUSEBUTTON_LEFT) {
if(this.bulletCntr != 0) {
// Process action
this.shoot();
// Decrease bullet count
this.bulletCntr--;
}
}
};
// 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);
// Play and reset muzzle flash
this.muzzleFx.particlesystem.play();
this.muzzleFx.particlesystem.reset();
if(this.entity.sound != null) {
// Play this weapons sound
this.entity.sound.play("fire");
}
// We hit something with collision
if(result) {
console.log("You Hit: " + result.entity.name);
this.handleImpact(result.entity,result.point,result.normal);
} else {
console.log("Nothing Hit");
}
};
// handle impact
GunShoot.prototype.handleImpact = function(rsltEntity, rsltPoint, rsltNormal) {
var bullet = this.bulletFx.clone();
this.app.root.addChild(bullet);
bullet.setPosition(rsltPoint);
Utils.setEntityDirection(bullet, rsltNormal);
// 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);
}
setTimeout(function(){ // There is a better way for timeout
bullet.destroy();
},500);
};