Next step to my tutorial:

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:

  1. 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)
  2. 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)) {
      
  }  
};

You can check the player’s position and rotation to see if the weapon would be touching whenever the mouse is clicked. If it’s close enough, then trigger the damage taken. Otherwise, then nothing happens. You’ll need to experiment with what values work in your project, how close or far away the character can be before the enemy is damaged.
Another thing to mention is that you need to check the enemy’s position for when the player will take damage. (I assume the enemy will be melee only).
The most basic way I can think of it is to do something like this:

if(Math.abs(playerPos - enemyPos) < weaponRange && this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT)) {
enemyDamaged;
}
else if(Math.abs(playerPos - enemyPos) < playerDamagedRange) {
playerDamaged;
}

This ensures that the enemy is only damaged when they are close enough, and when the player has clicked the mouse. If the enemy gets close enough without being attacked, the player is damaged. I’ll also suggest to use mouse.wasPressed() instead of mouse.isPressed() because if you leave it as mouse.isPressed(), then the player can simply hold down on the mouse button and technically always be clicking, and thus always attacking.

Hope this helps a bit.

1 Like

Yes that points me into the right direction but just to let you know enemyDamaged and playerDamaged wouldnt work because they are not assignments :slight_smile:

Well yah, that’s not actual code, I was just trying to get the point across

1 Like