[SOLVED] How to enable and disable an entity with a trigger

So if the player is in a certain area, He will have a flashlight, else he wont, any help?
https://playcanvas.com/editor/code/1165232?tabs=158588150
https://playcanvas.com/editor/scene/1903823

var Vent = pc.createScript('vent');
Vent.attributes.add('Flashlight', { type: 'entity'});

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

//Contact

// update code called every frame
Vent.prototype.update = function(dt) {
    Vent.prototype.onCollisionStart = function(result) {
    if (result.other.name == 'Player') {
        this.Flashlight.enabled = true;
    } else {
        this.Flashlight.enabled = false;
    }
};
};

// swap method called for script hot-reloading
// inherit your script state here
// Vent.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/

Hi @Brent_Reddick!

Line 13 till 19 is a seprated function and should not be placed inside the update function.

Maybe it’s better to use a trigger entity instead. A trigger entity has no rigidbody component. To activate a trigger entity you can use events like triggerenter and triggerleave.

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

Is there a way to apply a enter and leave TO a rigidbody?

You mean you need trigger with a rigidbody? For that you can use the collisionstart and collisionend, like you already did.

In both cases the other entity (the player entity) need to have a rigidbody.

Your are a big help so far, I think I’m getting close, can you tell me whats wrong with this?
https://playcanvas.com/editor/code/1165232?tabs=158590145
Im using How to enable and disable an image with a trigger? - #13 by Albertos to help me as well

Your script looks correct to me now.

On what kind of entity did you attach the script?

Also make sure the player entity has the name Player and not player.

It half works, it does not turn off when I leave the room
https://playcanvas.com/editor/code/1165232?tabs=158590145&line=17&col=22&error=true

https://launch.playcanvas.com/1903823?debug=true

https://playcanvas.com/editor/scene/1903823

this room

I solved the, issue.
I appreciate your help, thanks!

Your scene looks very good!

By the way, I suggest making it a little easier for yourself to work in your scene.
For example I would disable the roof entity in the editor and enable it by script in the initialize function somewhere.
I do the same for lightning in my project. When I work in the editor I want more light than in the actual game.

Thanks for the tip! Your compliment means a lot!

Oh and one more small thing I gotta ask, so it works great, but do you have any tips on making the flashlight equip transition look nice?

I’m not sure what you mean.

Like is there a way to animate it, like on collision enter a animation is played once, kinda like Minecraft inventory swap but my character with a flashlight

I had to check a Minecraft video to be able to know what you mean, but it looks like you want an animation when the flashlight entity is coming in view?

I think the best and easiest way is to use a tween and tween the position and rotation from the default state to the in view state.

https://developer.playcanvas.com/en/tutorials/tweening/