Hi I’m currently working on a game and I have multiple raycast scripts connected to the camera. When a player completes a stage (in this case; bubbleCounter == 6), then I want the raycast script to be disabled then raycast2 script enabled.
This is my current failed attempt for that section:
What is not working exactly? Do you got any errors?
Where do you define bubbleCounter? Is this counter working as expected? Also if this code is on the script you disable, I would try to switch line 5 and 6.
The counter is from another script attached, and it is working fine
This is they raycast code I wanted to disable after bubbleCounter = 6
var Raycast = pc.createScript('raycast');
// initialize code called once per entity
Raycast.prototype.initialize = function() {
Raycast.instance = this;
// Add a mousedown event handler
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.mouseDown, this);
// Add touch event only if touch is available
if (this.app.touch) {
this.app.touch.on(pc.EVENT_TOUCHSTART, this.touchStart, this);
}
this.on('destroy', function() {
this.app.mouse.off(pc.EVENT_MOUSEDOWN, this.mouseDown, this);
// Add touch event only if touch is available
if (this.app.touch) {
this.app.touch.off(pc.EVENT_TOUCHSTART, this.touchStart, this);
}
}, this);
};
Raycast.prototype.mouseDown = function (e) {
this.doRaycast(e);
};
Raycast.prototype.touchStart = function (e) {
// Only perform the raycast if there is one finger on the screen
if (e.touches.length == 1) {
this.doRaycast(e.touches[0]);
}
e.event.preventDefault();
};
Raycast.prototype.doRaycast = function (e) {
var from = this.entity.camera.screenToWorld(e.x, e.y, this.entity.camera.nearClip);
var to = this.entity.camera.screenToWorld(e.x, e.y, this.entity.camera.farClip);
// Raycast between the two points
var result = this.app.systems.rigidbody.raycastFirst(from, to);
if (result) {
var pickedEntity = result.entity;
pickedEntity.script.aiDisappear.pulse();
}
};
I think the problem is the event listeners. They are still alive even if the script is disabled. You need to add some code in the initialize function to destroy them when the script is disabled.
I suggest to change your logic as a workaround for this and do all raycast code in the same script. Just use the right code for the raycast using your statements.
Hi so sorry, I really cant figure out how to apply the principle to trigger fire depending on the script the raycast is in contact with. Is it possible to fully disable the event listeners along with the script instead?
I did some research and based on your script script above you only need to add the code below to your initialize function.
this.on('disable', function() {
this.app.mouse.off(pc.EVENT_MOUSEDOWN, this.mouseDown, this);
// Add touch event only if touch is available
if (this.app.touch) {
this.app.touch.off(pc.EVENT_TOUCHSTART, this.touchStart, this);
}
}, this);
You already have this for when the script is destroyed, but this will also support when the script is disabled.