[SOLVED] How to add healthbar that is affected by raycasts

This is the relevant code

Gun.prototype.Shootytwo = function() {
    for(i=0;i<this.pellets;i++)
    {
        if (this.app.mouse.isPressed(pc.MOUSEBUTTON_RIGHT))
        {
            this.bulletentitytwo.setLocalEulerAngles((Math.random() * this.accuracy) - (this.accuracy / 2), (Math.random() * this.accuracy) - (this.accuracy / 2), (Math.random() * this.accuracy) - (this.accuracy / 2));
        }
        else
        {
            this.bulletentitytwo.setLocalEulerAngles((Math.random() * this.inaccuracy) - (this.inaccuracy / 2), (Math.random() * this.inaccuracy) - (this.inaccuracy / 2), (Math.random() * this.inaccuracy) - (this.inaccuracy / 2));
        }
        this.bulletentitytwo.children.forEach((enabloid) => {
            enabloid.enabled = true;
            enabloid.setLocalEulerAngles(0, 0, Math.random()*360)
        });
        anglePos = this.bulletentitytwo.forward.clone();
        anglePos.scale(this.range);
        from = this.bulletentitytwo.getPosition();
		result = this.app.systems.rigidbody.raycastFirst(from,anglePos.add(from));
		if(result)
        {
		    if(result.entity.tags.has(this.enemytag))
            {
		            result.entity.translateLocal(0,-50000,0);
			        result.entity.sound.play("sound");
                    Grams += this.damage;
                    //Grams += ((Math.floor(Math.random() * 6)) + 1);
		    };
        };
    }
};

Currently it just translates the entity as a placeholder. Can anyone tell me how to have individual entity specific health and to lower it by a given amount?

1 Like

Hi @ALUCARD!

I decided to update my basic shooting example with an enemy that has a health bar.

With the collision of the bullet I execute the function on the enemy to update the health bar. You can basically do the same with your raycast hit.

https://playcanvas.com/project/1267420/overview/basic-shooting

2 Likes

Using the physical size of an entity as the health? That seems very floating point errory. Actually it might be better if the code changed a hidden text entity so precise health could be achieved, either that or parsefloat the name.

This is an example I made. You can change it anywhere you want to make it more stable or whatever.

I already made a far superior version. I believe the entire example will be finished within a few days. Also mark this as solved.