Getting The tag of a cliked gameObject

Hi everyone! So my problem seem simple but i’m really having some problems with it. Basically, when i click on an entity, it must check of it’s tagged “Case”. As simple as that! but as i don’t work with unity3D usually, plus raycasts, plus tags… You can guess i’m a bit lost! So a little help would be appreciated! Thank you all!

So far, i’ve got:

void Update(){

        if (Input.GetMouseButtonDown (0)) {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast (ray, out hit, 100)) {
                Debug.Log (hit.transform.gameObject.name);
            }
        }
    }

So at this point, i just want to test the raycast, but it won’t work… ^^’

Hi,
Try This,

Hi, Thank you! With this, i can define more proprely why i can’t make this work, and explain it:
A raycast needs an origin point and a direction. My origin point is the camera, and my direction is “towards the place i clicked”
but, as i’m in 3d, with perspective, how to get this direction?

What you have there is already correct, and it works fine for me, as long as the objects aren’t more than 100m away fro the camera. What’s the specific problem? Are you not seeing your Debug.Log being printed?

Try adding this to your code, just after you create the ray:

Debug.DrawRay(ray.origin, ray.direction * 100, Color.red, 30f);

That will draw a line (in the Scene view) every time you click, so you can see where the ray is going.

Note that Raycast only hits colliders, and potentially only non-trigger colliders depending on your project settings. Do you have colliders on the objects you’re interested in hitting?

Ultimately, after you get your successful hit, you’ll want the following to test its tag:

if (Physics.Raycast(ray, out hit, 100))
{
    Debug.Log(hit.transform.gameObject.name);
    if (hit.collider.gameObject.CompareTag("Case"))
    {
        // We hit a case. Do whatever.
    }
}

Yeah, the debug.log isn’t printed.
Yeah, i have colliders, but basically what a “Case” is, is a square space on a checkerboard, and they stick up a bit from the board… Anyways, i want them to be trigger, so they don’t physically interfere with the pawns i’ll put. So how can i do that?

Plus, here is my code anyway:

{

    private Vector3 CaseCoord;
    private GameObject[] cases;

    void Start()
    {
        cases = GameObject.FindGameObjectsWithTag("Case");
    }
    void Update ()
    {

        if (Input.GetMouseButtonDown (0)) {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            Debug.DrawRay (ray.origin, ray.direction * 100, Color.red, 30f);

            if (Physics.Raycast(ray, out hit, 100)) {
                Debug.Log (hit.transform.gameObject.name);
                if (hit.collider.gameObject.CompareTag ("Case")) {
                        Debug.Log ("on fais la bise");
                }
            }
        }
    }
}

And with your debug line, it says it’s “not set to an instance of an object”.

If you go to Edit → Project Settings → Physics, there’s a checkbox to allow queries to hit triggers. You’ll need to turn that on for Raycasts to hit triggers.

I’m not sure how a Raycast hit could be missing a collider… I guess you can always use hit.transform instead, assuming that works.

The checkbox was already checked!
Now, the line that’s not set to an instance of an object is this one:

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);


And three times, in one click, i get this. Plus, i only get that to the first click, not the other ones.

hi,
I got this error when my camera tag is not set MainCamera. Please check out the tag of the Main Camera.

void Update()
{

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, 100))
print(“Hit something!”);
}

Oh my, seems i didn’t know about this either! Thank you so much, it works perfectly now :slight_smile:
And… If i want to get the coordinates of the hit objects, from their transform component, what should i do…?

The RaycastHit object contains a “point”, which is the exact 3D point in space where the object was hit. Often that’s what you’ll want. Otherwise, the hit has a ‘collider’ property, and you can get the center of the collider if that’s what you want. Or the transform of the object you hit is an option. It depends on what you mean by “coordinates”.

By “coordinates”, i mean proper x,y and z coords of the object i hit :slight_smile:
Isnt “hit.transform.z” supposed to work?

If you mean hit.transform.position, that will just give you the position of the object you hit, not the exact point that was hit. Often, the “position” of an object is just its center, which may or may not be what you want.

hit.point, on the other hand, is the exact position in space where the raycast hit the object. It’s almost always what I’m looking for from a raycast,

Yes! I wanted the correct syntax, and you gave it to me, thanks! it’s all working as i want now :slight_smile: