2 bullet hits will destroy the entity

Thank you guys all your help has giving me a lot of progress and i appreciate all of you.

But i am having problems in coding with destroying the entity, there’s no life on the enemy, after two hits.
what should i add in this code?

//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");
}

};

Hi @Kwangbae_IZONE,

What does this.handleImpact() do? Would you be able to post a link to your project for others to view?

I would guess you would want to keep track of the number of times the entity has been hit within that function by updating a value in the initialize function. You could do something like create this.hits in your initialize funtion and set it equal to 0 than that add to it in this.handleImpact() with this.hits++; Then you couldcheck to see how many times the entity has been hit and if it has been enough times call .destroy() on the entity.

I hope this is helpful.

1 Like

@Kwangbae_IZONE I think the code snippet above is from this example that I have published and recommended as a good starting point.

https://playcanvas.com/project/990658/overview/weapontest

You should really have to add much to the gun-shoot code at all since the this code is below to handle this function call.

// 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);

};

What this code is doing is actually cloning some items like bullet holes etc and creates the effect in the scene. Also take a look to some of the variables you will need to supply as well to get the effects to work.

image

1 Like

Thank you very much for this. I kinda get how it works now bc of that.

Understood thank you very much

1 Like