How To Detect Mouse Click On GameObject

Share on social media

In this article, I will show you a couple of ways to detect mouse click on gameObject specifically on the 3D gameobjects.

Many of you may are having the same questions like “How do I know if my mouse click is over the gameobject?” and so many similar queries you may have. So let’s see how you can achieve the same.

Detect Mouse Click On GameObject

Ways To Detect Mouse Click On GameObject

There are a couple of ways to detect 3D gameobjects on mouse click in unity but in this article, we will mainly look into 2 ways to detect mouse click over gameobject.

So let’s quickly look at the ways to achieve the same.

# Method 1

In this method, We will see how we can detect mouse click on GameObject with raycast. This is a really common method to perform this kind of task.

Let’s look at the code:

using UnityEngine;

public class ObjectDetection : MonoBehaviour
{
    
    private Camera camera;
    
    // Start is called before the first frame update
    void Start()
    {
        camera = Camera.main;
    }

    // Update is called once per frame
    void Update()
    {
       DetectObjectWithRaycast();
    }

    public void DetectObjectWithRaycast()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = camera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                Debug.Log($"{hit.collider.name} Detected",
                    hit.collider.gameObject);
            }
        }
    }
}

So let’s understand the above code, We are creating raycast on the mouse left click.

  • First, we took the Camera because we will need it in the next steps. 
  • Then we created the ray and converted the screen mousePosition to ray through the API of camera ScreenPointToRay
  • In the last, we call the raycast function, and when it successfully hits any gameobject so it debugs its name.
So this was the first method to detect mouse click on GameObject let’s see the other way to do the same.

# Method 2

This method is simpler to detect mouse click on GameObject but it has its limitations as well. In this method,  We will use unity’s OnMouseDown Method.

Let’s look at the code:

using System;
using UnityEngine;

public class ObjectDetection : MonoBehaviour
{
    private void OnMouseDown()
    {
        Debug.Log("Mouse Click Detected");
    }
}

Simple Code, Right?

Let’s understand how to use it. Unlike the above raycast code, This will only work if this script attaches to the gameobject that you want to get detected through a mouse click.

So it means, If you have 10 different gameobjects that should be intractable via mouse click, then all 10 gameobjects must have this script.

So this was the second and simpler way to detect mouse click on GameObject. But now let’s talk about the conclusion and which way is best for you.

Note: In both the above methods, The gameobject that you want to detect must have a collider.

Conclusion

If you are still reading this post, then you must be thinking that when to use which method.

Don’t worry, Let me tell you which will be the best option for you.

Choose #Method 1 If:

  1. You are looking for something that you want to attach only one script to the whole scene.
  2. You want to detect multiple types of gameobjects, maybe with the tag or layer.
  3. You want to perform different actions on different gameobjects.

Choose #Method 2 If:

  1. Your action is the same on all the gameobjects.
  2. The objects are fewer to detect with a mouse click and don’t want to mess up the click event in GameManager and want to make this independent.

Now you should be able to decide whether you should choose #Method1 or #Method2 to detect mouse click on GameObject. But still if you have any doubts or queries feel free to let me know in the comments I will try my best to help you.

I hope the article was helpful and you have learned how to detect mouse click on gameObject(3D). Comment your thoughts or query in the comment section below. And also subscribe to our blog if you want to get this type of informative content related to game development.

Aakash Solanki
Aakash Solanki

I am a professional unity game developer. I have a passion for creating immersive gaming experiences. For me writing articles feels like giving back to the community we have learned from. My hobbies involve playing games and writing articles and sometimes I also create youtube videos.


Share on social media

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top