[SOLVED] How to Enable and Disable entity on mouse click

i would like to know how to enable an entity and disable one when holding down the mouse button right click, when i’m holding it down i want it to enable one entity and disable another entity but when i stop holding it down i want it to do the disable and enable the opposite entity is enabled/disabled before

my aim is to turn this into an ads feature so i can use this to make an ads overlay when i’m holding my right mouse button, also a project example would be nice

if you add a little changable zoom function that would be nice
and also can you make it only work when the entity with that script is being held

Hi @Deadshot1m24!

You can check the page below to learn more about mouse input. You can easily modify this script to achieve your goal.

https://developer.playcanvas.com/en/tutorials/mouse-input/

Not sure what you mean with this.

What do you mean with entity? A model or (button) element?

do you have a specific script that can enable and disable entities on a mouse click?

Enable an entity:

entity.enabled = true;

Disable an entity:

entity.enabled = false;

no i mean like how analyze when you hold the mouse down and when you hold the mouse up

You can find a script example on the page that I shared in my first post.

i dont see one i can use properly

Okay, maybe the page below can help you then. Please check the full page because there are different approaches.

https://developer.playcanvas.com/en/user-manual/user-interface/input/

ok took a little look and scrapped this together
can you tell me why i’m getting errors

image

Line 6 to 10 should be in the update function that is present when you create a new script. You probably deleted that piece of code. Apart from that line 10 should be } instead of };.

how do you put that in the update function thing ?

ADS.prototype.update = function (dt) {
    if (this.app.mouse.isPressed(pc.MOUSEBUTTON_RIGHT)) {
        this.levelscreen.enabled = false;
        this.playscreen.enabled = true;
    }
};
1 Like

now what script would make it to where when you stop pressing the right mouse button it reverts it back to normal

it only works when holding the sniper so :slight_smile: that works intentionally but it gets stuck to the screen and you cannot shoot while using ads (i can fix the shooting)

so? also if you could add a zoom factor that would be great

I think you can do kind of the same with using wasPressed instead of isPressed.

Sorry, I have no idea what you mean with this.

the waspressed thing doesnt work

the waspressed idea didn’t work and forget about the zoom thing anything else that’ll work?

What was your setup and result?

image

this is my code, and it worked the same, it worked when i pressed it but when i stopped pressing it it didnt stop Aiming