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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

What's the best way to click on objects in game

Discussion in 'Scripting' started by InsensitveJ0ker, Mar 21, 2020.

  1. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    I've looked up some videos involving this, and they said to use rays. But for some reason the object isn't detecting the click. I'm not sure if I'm supposed to put the script into the object or some other object. Or even if I'm supposed use rays.
    Code (CSharp):
    1. public class Swap : MonoBehaviour {
    2.  
    3.     public GameObject swappedInto;
    4.  
    5.     public Transform blankspace;
    6.     void update()
    7.     {
    8.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    9.         RaycastHit hit;
    10.         //print("hello 1");
    11.         if (Physics.Raycast(ray, out hit) && Input.GetMouseButtonDown(1))
    12.         {
    13.  
    14.             if (hit.transform.position == transform.position)
    15.             {
    16.                 print("hello");
    17.                 Instantiate(swappedInto, transform.position, transform.rotation);
    18.                 Destroy(gameObject);
    19.             }
    20.         }
    21.  
    22.  
    23.     }
    24.    
    25.  
    26. }

    This script is supposed to swap one object into another object when it's clicked on.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    For Raycast to work there has to be an enabled collider on the things you wanna hit.

    Your GetMouseButtonDown(1) also checks the middle button. Button zero is the primary left click:

    https://docs.unity3d.com/ScriptReference/Input.GetMouseButtonDown.html

    In fact in most programming contexts, zero is almost always the first item, but not always.

    Also, checking two Vector3 quantities for equality is not generally a good practice (line 14), since they can be vanishingly different and appear the same, and they test not equal in that case.

    I think in this use case you are actually checking it for identifying itself, so that might be better accomplished with a tag or perhaps just comparing the transforms directly.
     
    Lethn likes this.
  3. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    @Kurt-Dekker Beat me to it, I would use an if statement tag check as well and I also don't see any need for the && operator, you can just put the input inside the raycast and it will work fine.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    I think he might need it, otherwise just moving the mouse over an object will zap it, right? I guess for a mobile touchscreen it would work...

    OP, probably best if you wrap ALL of this code in a check of the mouse button, removing the mouse button check from where Lethn pointed out the && block.

    This way, all this raycasting and whatnot isn't happening every frame, only on the frame you click on.
     
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Ahhh okay, that's an optimisation thing to consider then isn't it? Will keep that in mind.
     
  6. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    How would I check for tags?
     
  7. InsensitveJ0ker

    InsensitveJ0ker

    Joined:
    Feb 1, 2019
    Posts:
    20
    How would I put the input inside the raycast
     
  8. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
  9. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,926
    Update, with a capital U. Yours is lowercase.
     
    Kurt-Dekker likes this.