[SOLVED] Need to disable or enable a script when collision enter and leave. Need your help please :)

Hello :slight_smile:

I hope you are well. Today i need your help because this script is working only in one way.

Can you tell me what i’m doing wrong please ?

Thanks for your help !

Antoine

Here is my code :

var Letestingnon = pc.createScript('letestingnon');

// initialize code called once per entity
Letestingnon.prototype.initialize = function() {
    
var DesEntity3 = this.app.root.findByName("supertonton");
DesEntity3.script.enabled = false;
DesEntity3.script.iframePlane.enabled = false;
console.log("false");  

this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
this.entity.collision.on('triggerleave', this.onTriggerLeave, this);
var app = this.app;
};

// update code called every frame
Letestingnon.prototype.update = function(dt) {  };

Letestingnon.prototype.onTriggerEnter = function(entity) {
        
 var DesEntity3 = this.app.root.findByName("supertonton");
DesEntity3.script.enabled = false;
DesEntity3.script.iframePlane.enabled = false;
console.log("false");  
        
        
};
    
Letestingnon.prototype.onTriggerLeave = function(entity) {
        
 var DesEntity3 = this.app.root.findByName("supertonton");
DesEntity3.script.enabled = true;
DesEntity3.script.iframePlane.enabled = true;
console.log("true");  
        
};  
    
    

Hi @N1MP,

Are you getting any errors? Or the trigger enter and leave methods aren’t firing at all?

Have you added a collider component to your entity to act as trigger?

1 Like

Hello dear Leonidas,

how are you today ? ça va ?

i prepare to you a YT video to show you the problem :
https://youtu.be/nQbsk7h0EBk

Yes i put a collider on my player too.

Yes i can see two errors :

Stratégie de référent : les stratégies les moins restrictives, comprenant « no-referrer-when-downgrade », « origin-when-cross-origin » et « unsafe-url » seront bientôt ignorées pour la requête intersite : https://amb-production.com/diaporama/index555.html index555.html

**ERROR 1 : when enterring inside area **
Error: An unexpected error occurred spoofer.js:1:38935
Le script à l’adresse « https://widget.time.is/r/?nofollow.w0.h39. » a été chargé alors que son type MIME (« text/html ») n’est pas un type MIME JavaScript valide.

**ERROR 2 : when leaving area **
Error: Promised response from onMessage listener went out of scope

So the first entry is working, but the leaving is not working, and the second entry is not working, etc.

So i don’t understant…

Thanks for you help

Antoine

This is likely because you’re disabling the script that the onTriggerEnter and onTriggerLeave functions are on. The script needs to be enabled in order for the collision callbacks to fire.

I’d recommend that you don’t disable the script entirely. Keep the script enabled and set a boolean isColliding attribute inside the onTriggerEnter and onTriggerLeave functions. Then in other parts of your code, you can use the isColliding value to enable/disable functionality.

1 Like

Here is my project, can you look it please ?

https://playcanvas.com/project/846246/overview/4x3

Thank u Devortel for your reply. As you can see on this screenshort, i try to control supertonton by name, and i want to activate it (iframePlane from supertonton entity) when entering area and disable “supertonton” script when i leave the aera. I share with you and with Leonidas my project, maybe you can look at it and tell me what’s wrong ?
Thanks a lot for your help, i appreciate ! :raised_hands:

I just ran the project and it does seems to work for me. Trigger enter function, as well as trigger leave function, is working.

2 Likes

Hi Saif ! how are you today ? :slight_smile:

What i want to do is to switch on the screen when enter the area and switch off the screen when leaving area. And i want to do it in loop of course :slight_smile: switch on TV then switch off TV, switch on TV, etc. This is unfortunately not working… :cry:

Thanks for your help !

Hey, so the screen has a html element which can not be hide by just disabling the script. You will have to find the element of the video and then disable it’s view.

You’ll want to avoid disabling the script that’s controlling the TV because this also disables all the functionality that the script contains. I’d recommend keeping the supertonton script enabled in the editor and then simplifying your letestingoui.js functions like so.

Letestingoui.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
    this.entity.collision.on('triggerleave', this.onTriggerLeave, this); 
};

Letestingoui.prototype.onTriggerEnter = function(entity) {
    var DesEntity2 = this.app.root.findByName("supertonton");
    DesEntity2.model.enabled = true;    
};
    
Letestingoui.prototype.onTriggerLeave = function(entity) {     
    var DesEntity2 = this.app.root.findByName("supertonton");
    DesEntity2.model.enabled = false;
};

Note: This code is disabling the model which hides it from view, the iFrame is still ‘loaded’ behind the scenes it’s just not visible to the user.

Thank u so much Devortel, but i need to disable the script because it make a conflict with another script.

when the script is off my other HTML script is working. But the two script together are not working :rofl:

Hey @N1MP , i have resolved the issue for you in this project : https://playcanvas.com/editor/scene/1262677
Basically the html elements doesn’t disable with disabling the entity so you have to find the element by id and then enable/disable it.

You can read more about the document methods from here : Document - Web APIs | MDN

1 Like

Thank u, i this is a nice method to show or not to show that is the question ha ha ^^

but what i need is to disable the script, then to enable the script. Because it make a conflict with another script. When both script are on, it doesn’t work. So what i need to do is to disable the script when i’m in the area zone collider of the second script to disable the other to cancel the script conflic.

First script : iFRAME on a plane
Second script : HTML content shown on the screen

When the two scripts are “ON” it’s not working. But if the iframe script is OFF there is no problem.
Maybe i can create a scene to show you this ? what to you think ? :slight_smile:

Thanks for all your help dear Saif !

Antoine

I change my scene to show you my original problem :yum:

https://playcanvas.com/project/846246/overview/4x3

Right now, your problem is that when you’re disabling the iFrame, you’re also disabling the collision component as well as the onTriggerEnter and onTriggerLeave callbacks.

If the iFrame script has to be disabled. You need to put the collision component AND the script containing the onTriggerEnter and onTriggerLeave functions on a different entity that doesn’t get disabled with the iFrame. I’d recommend keeping the collision component and script where it is and then moving the iFrame script into a new, child entity. Then you can enable/disable the child entity without affecting the parent’s collision.

1 Like

Thank u Devortel, i will try your solution thank u very much ! will keep you updated :raised_hands: thanks for your time

I’ve made an example here that does what I think your intentions are?

(press left/right to move the ball into the cube trigger)

https://playcanvas.com/project/846331/overview/show-hide-on-trigger

1 Like

Looking at your modified iframeplane code, you are ultimately missing the logic that listens for when the script instance is disabled/enabled to hide/show the iframe.

From the original example: https://playcanvas.com/editor/code/751877?tabs=40612425

You can see here it listens to those events and reacts accordingly.

1 Like

Solved : by adding this dom argument :
document.body.insertBefore(this.div,document.body.firstChild);

Thank u for all members, for all your help !
Thank u : yaustar; devortel, saif and leonidas :raised_hands:

1 Like