Ok so this is what i have so far on my health tutorial: https://playcanvas.com/project/605986/overview/tutorial-health-bar--main-menu
So far i think its pretty good, but now i want to take it a step further:
- i want to add a weapon (That works) and right now the enemy dies whenever you continuously click the mouse… i will want to only implant health loss when the weapon hits the enemy (I will work on this today)
- i will add player health diminishment soon also…
if anyone has any suggestions or maybe some help with the weapon please let me know here is the code for the health on the enemy:
var Health = pc.createScript('health');
// initialize code called once per entity
Health.prototype.initialize = function() {
this.camera = this.app.root.findByName('Camera');
this.textElement = this.entity.findByName('nHealth');
this.weaponEntity = this.entity.findByName('sword');
this.healthEntity = this.entity.findByName('eHealth'); // we're searching inside the entity
//var scale = new pc.Vec3().copy(this.healthEntity.getLocalScale()); // store the scale inside a new vec3 (scale is a vec3)
//var rotate = new pc.Vec3().copy(this.weaponEntity.getRotation());
this.takeDamage = 0;
};
// update code called every frame
Health.prototype.update = function(dt) {
this.entity.setRotation(this.camera.getRotation());
var scale = new pc.Vec3().copy(this.healthEntity.getLocalScale()); // store the scale inside a new vec3 (scale is a vec3)
if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
scale.x = pc.math.lerp(scale.x, 0, 0.5); // SCALE DOWN
} else {
scale.x = pc.math.lerp(scale.x, 1, 0.001); // SCALE UP
}
this.healthEntity.setLocalScale(scale);
if (scale.x < 0.01) {
scale.x = 0;
this.entity.script.dead.dead = true;
}
if (scale.x > 1) {
scale.x = 1;
}
if (this.takeDamage > 0) {
scale.x -= this.takeDamage;
this.takeDamage = 0;
}
this.healthEntity.setLocalScale(scale);
};
Health.prototype.destroy = function() {
if (this.app.root.Enemy(isDead)) {
}
};