Home > Software design >  Unity Right Click On Object
Unity Right Click On Object

Time:03-10

I am new to unity. Currently, I am working on a small project.

I am creating a solar system where the player can click on planets and the main camera will zoom in and follow that planet.

The player can also right-click on a particular planet and the name of that planet will be displayed. However, if the player re-clicked the right click again, the name will disappear (need help with that)

I used onm ouseOver with Input.GetMouseButton (see below). But I am stuck and have no idea how to stop displaying the text when the player re-click right-click.

Can anyone please help me :)

What I have used:

private void onm ouseOver()
    {
        if (Input.GetMouseButtonDown(1))
        {
            text.SetActive(true);
        }
        
    }

CodePudding user response:

I think it'd be good to use Raycast.

If you want to float text that fits the planet, you will need to connect it within the script on the object.

I'm sorry if the meaning was not conveyed well using a translator.

CodePudding user response:

You can use 1 variable to storage the current state and switch on/off state.

bool active = false;
private void onm ouseOver()
    {
        if (Input.GetMouseButtonDown(1))
        {
            active = !active;
            text.SetActive(active);
        }
        
    }
  • Related