[SOLVED] Disable entity on collision enter and disabling it on collision leave

I have a project where I need an entity to be disabled on collision enter and be disabled on collision leave. Any way to tweak the normal given original collision enter and leave script to do what I need?

Any help is highly appreciated.

Hi @Koma and welcome! I don’t think you need to disable an entity that is already disabled, so can you rephrase your question please? What are you trying to achieve?

1 Like

Actually, your question may be correct. In that case, the script will look something like below. The script could be written with even less code, but I think this way the the script remains the most understandable.

var Collider = pc.createScript('collider');

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

Collider.prototype.onCollisionStart = function (result) {
    this.entity.enabled = false;
};

Collider.prototype.onCollisionEnd = function (result) {
    this.entity.enabled = false;
};
1 Like

thanks for your help. I am glad to see that somebody is willing and able t help us little guys with our projects.

1 Like
var Collider = pc.createScript('collider');

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

Collider.prototype.onCollisionStart = function (result) {
    this.entity.enabled = true;
};

Collider.prototype.onCollisionEnd = function (result) {
    this.entity.enabled = false;
};

here is what i got, now that the entity is disabled after 1 second can you make it enable again?

also can you make on collision enter disable two different selectable entities, and enable one other selectable entity?

as a diffrent script.

Yes, you can create a timer or use a timeout like below.

Collider.prototype.onCollisionEnd = function (result) {
    this.entity.enabled = false;

    setTimeout(function(){
        this.entity.enabled = true;
    }.bind(this), 1000);
};

Yes, you can replace this.entity with for example this.app.root.findByName('NameOfAnotherEntity').

1 Like

thanks, this help is much appreciated.

A post was split to a new topic: How do I use the name of entity when disabling?