Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Click on object

Discussion in 'Scripting' started by Tornado77, Dec 20, 2018.

  1. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    Hello, im making a trader for my game but i dont know how to detect if i clicked on the trader, i want to use raycast for that but i dont know how to detect what object have been clicked
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    How to do it is highly dependent on what the trader is: a 3D object, a sprite, a piece of UI, etc. There are plenty of tutorials for doing it each different way.
     
  3. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Excellent. You'll probably be raycasting from the mouse (using the camera to determine the ray), and hitting a collider on (or around) the trader. Plenty of tutorials and even more examples out there... give it a shot, post your code back here (with formatting) if you have specific problems implementing.
     
  5. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    Its working! but i am using the Unity firstpersoncharacter, and when i click on button, the cursor become invisible

    Code (CSharp):
    1. void OnMouseDown()
    2.     {
    3.         panel.SetActive(true);
    4.         PanelOpen = true;
    5.         Cursor.visible = true;
    6.         gun.CanShoot = false;
    7.     }