[SOLVED] Add collision without rigidbody?

Hi. I have been working on my game, and I have a question about physics and collision. I am using @Albertos enemy AI, and all is well, except one thing. Since the enemy’s bullet has collision but no rigidbody, I am not sure on how to add collision. I want to add in my fps movement script so that when the player gets in contact with the enemy bullet, it takes away health. Right now, since the bullet doesn’t collide, the only way to lose health is if the enemy collides with the player. Does anyone know how to do this? Also, the player has a dynamic rigidbody component. PlayCanvas 3D HTML5 Game Engine

// this code deteccts the enemy's rigid body and then takes away health. 
//How could i do this for the enemy bullet that doesnt have a rigidbody?
FirstPersonMovement.prototype.onContact = function(result){
    if(result.other.rigidbody && result.other.tags.has('enemy')) {
        var self = this.entity;
        this.health -= 50;
        console.log(this.health);
        this.textEntity.element.text = "Health: " + this.health + "/100";
    } 
}; 

Hi @almish_subuk!

If an entity has a collision component but no rigidbody component, it acts like a trigger. The code for triggers is a little bit different, but you can still check for collision. You can find code examples on the page below.

https://developer.playcanvas.com/en/tutorials/collision-and-triggers/

Alternatively, you can add a kinematic rigidbody to the bullet. I didn’t do this, because the bullet will add a force to dynamic rigidbodies when they hit each other.

that is what i am looking at. i will also try to add a kinematic rigidbody. thanks!

this may seem like a dumb question, but how can i make it so that the event is only fired when the player touches the trigger?

EnemyBullet.prototype.onTriggerEnter = function(entity) {
//how can i make it so that this only fires if the player touches the bullet?
  console.log("you touched the enemy's bullet");
};

You can check for a name or tag.

if (entity.name === 'Player') {

}
if (entity.tags.has('player')) {

}

thanks! I can now make more progress on my game thanks to you! :slight_smile:

1 Like

Are you working in a branch? I can’t see you use my enemy AI and I can’t see a bullet entity in your project.

oh! i am using branch ‘am’. sorry. Also, how can i call a function or set the player health attributes from a different script? because when the player gets hit by the bullet, i would like it to remove 10 health. this is what im using right now:

EnemyBullet.prototype.onTriggerEnter = function(entity) {
    if (entity.tags.has('player')) {
        console.log("amongus");
        entity.script.firstpersonmovement.setHealth();
    }
}; // first-person-movement is the name of my script
// here is the function
FirstPersonMovement.prototype.setHealth = function() {
    if(this.health > 0) {
        this.health -= 10;
    }
};
//this will only fire for the enemies bullet

Other users are unable to see branches.

What you do is correct, but your script name is firstPersonMovement instead of firstpersonmovement.

:man_facepalming: my bad. also, i am going to merge branches soon. Thanks for the help!

1 Like