Change text element by touching with a bulllet

i need something like HP when bullet connected with player reduce hp

var TriggerVolume = pc.createScript('triggerVolume');

TriggerVolume.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', function (entity) {
        if (entity.name === 'BULLET') {
           //exam element= exam element -1
        }
    });
};

i need code for here //exam element= exam element -1

Hi @SUPER_ME! On which entity do you attach the script?

player

I guess something like below.

// initialize
this.playerHealth = 10;
// trigger
this.playerHealth--;
textEntity.element.text = this.playerHealth;

where should i add them ?
here is project PlayCanvas 3D HTML5 Game Engine


change this

Which part you don’t understand?

where i add them to my code?

I mentioned that above the code? Apart from, that I guess your player entity has a rigidbody, so you can’t use it as a trigger.

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

any good exams?

I don’t have an example in mind. It’s very simple, you have almost everything you need.

i don’t know anything of java script i only know java and python

You can create a new bulletCollider.js script and replace the code with the code below. Add the script to your player entity that has a collision component and a rigidbody component.

var BulletCollider = pc.createScript('bulletCollider');

// initialize code called once per entity
BulletCollider.prototype.initialize = function () {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);

    this.playerHealth = 10;
    this.textEntity = this.app.root.findByName('HP');
    this.textEntity.element.text = this.playerHealth;
};

BulletCollider.prototype.onCollisionStart = function (result) {
    if (result.other.name === 'BULLET') {
        this.playerHealth -= 1;
        this.textEntity.element.text = this.playerHealth;
    }
};