let clickCounter = 0;
// Get a reference to the entity
const entity = document.querySelector('#my-entity');
// Add an event listener to listen for clicks on the entity
entity.addEventListener('click', function() {
// Increment the click counter
clickCounter++;
// If the click counter is 5, disable the entity
if (clickCounter === 5) {
entity.disabled = true;
}
});
Please tell me if it will work or not. Thank you in advance.
I’m trying to make it so that when the entity is clicked on 5 times it becomes disabled.
Hi @Jacob_McBride2! Where did you find this script? I’m not sure about line 4. Has the entity an element component or is it a 3D model?
1 Like
I made the script, but as you can tell my code is a mess, and the entity is a 3D element
Check the page below to see how you can click on 3D entities.
https://developer.playcanvas.com/en/tutorials/entity-picking/
1 Like
Can you fix my script please?
Not tested, but I guess it will be something like below. The entity need this script, a collision component and a rigidbody component.
var PickerRaycast = pc.createScript('pickerRaycast');
// initialize code called once per entity
PickerRaycast.prototype.initialize = function() {
this.cameraEntity = this.app.root.findByName('Camera');
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onSelect, this);
this.on('destroy', function() {
this.app.mouse.off(pc.EVENT_MOUSEDOWN, this.onSelect, this);
}, this);
this.clickCounter = 0;
};
PickerRaycast.prototype.onSelect = function (e) {
var from = this.cameraEntity.camera.screenToWorld(e.x, e.y, this.cameraEntity.camera.nearClip);
var to = this.cameraEntity.camera.screenToWorld(e.x, e.y, this.cameraEntity.camera.farClip);
var result = this.app.systems.rigidbody.raycastFirst(from, to);
if (result && result.entity === this.entity) {
this.clickCounter++;
if (this.clickCounter === 5) {
this.entity.enabled = false;
}
}
};
2 Likes
Ah I see. Okay thank you for telling me and helping me.
Can you make the screen shake when you click on an entity? It would make the script feel more responsive.