3
\$\begingroup\$

I am trying to check if a GameObject is clicking another GameObject. I am trying to make a 2D shooter like Duck Hunt so this is important. I have tried IPointerDownHandler but found out that it is no longer supported. Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using UnityEngine.EventSystems;

public class ShootScript : MonoBehaviour//, IPointerDownHandler
{
    private Vector3 mousePosition;

    void Start()
    {
        gameObject.SetActive(true);
    }

    void Update()
    {
        /*mousePosition = Input.mousePosition;
        if (mousePosition.x == transform.position.x && mousePosition.y == transform.position.y && Input.GetMouseButtonDown(0))
        {
            gameObject.SetActive(false);
        }*/
    }

    /*public void OnPointerDown(PointerEventData pointerEventData)
    {
        gameObject.SetActive(false);
    }*/
}

I have a GameObject "Shooter" and a GameObject "Enemy". I want to check if the shooter is clicking the enemy.

Unity version: 2019.4.40f1

Thanks!

\$\endgroup\$
4
  • \$\begingroup\$ There seems to be some confusion you are having. IPointerDownHandler is used, if you are clicking on something. But what you are describing, you want to detect collision docs.unity3d.com/ScriptReference/… or a raycast if your bullets are instant. One GameObject is never clicking on a second one \$\endgroup\$
    – Zibelas
    Apr 16 at 9:05
  • \$\begingroup\$ Sorry about that, I had tried to use another way using a raycast, but it did not work. It might be a bit confusing. \$\endgroup\$
    – ok-coder1
    Apr 16 at 10:46
  • \$\begingroup\$ As Zibelas says, it does not make sense for "one game object to click on another". The player can click their mouse on an object. So it's not clear what you mean when you say you want the shooter object to do the clicking. Can you clarify? There's also a lot of nonsensical code here. gameObject.SetActive(true); doesn't make sense to put in Start() because that function only gets called if the game object is already active. Your commented-out code in Update() is comparing the mouse position in screen space to a game object position in world space: these are not measured on the same grid. \$\endgroup\$
    – DMGregory
    Apr 16 at 13:10
  • \$\begingroup\$ Sorry about the confusion here, I meant the cursor to click on the GameObject. And for the code, I deleted everything I didn't need. Check the comment on the answer below. \$\endgroup\$
    – ok-coder1
    Apr 16 at 13:36

2 Answers 2

3
\$\begingroup\$

In order for an object to be detectable by the collision detection system of the engine, it needs a collider component. There are both 2d and 3d colliders that come in different shapes. So you need to pick the one that's most appropriate for the shape of your object and then adjust its dimensions accordingly.

After you added a collider to an object, there are different ways to detect collisions with it:

  • If it's about detecting mouse cursor clicks, then having a script that implements the IPointerClickHandler interface isn't a bad approach. No, it's not obsolete. It was just moved from the engine core to a package, just like the rest of the UI system. You now find it in the documentation for com.unity.ugui.

    In order for it to work, it requires two additional things to be present in your scene:

    1. a Physics Raycaster and/or Physics 2D Raycaster component on your camera (depending on whether you want to detect 3d or 2d colliders).
    2. An Event System component somewhere in the scene.
  • If it's not about mouse cursor clicks (or at least not directly), you can use the Physics class (for 3d colliders) or Physics2D class. They offer several methods to detect all colliders currently overlapping various geometrical shapes.

  • You can mark colliders as triggers. When you do that, then scripts on these objects will receive OnTriggerEnter, OnTriggerExit and OnTriggerStay messages. Note that these will only trigger for objects that have Rigidbody components in addition to a collider.

  • When both colliders have rigidbodies (or when one of them is static), which means that the engine treats them as solid objects, then they will also trigger OnCollisionEnter, OnCollisionExit and OnCollisionStay messages as soon as they get into contact. These messages have the advantage over OnTrigger messages that they contain a lot more information on how exactly the collision occured.

More information in the manual.

\$\endgroup\$
6
  • \$\begingroup\$ Debug.Log(name + " Game Object Clicked!"); in docs.unity3d.com/Packages/[email protected]/api/…, is name the name of the object clicking it or the name of the object clicked on? \$\endgroup\$
    – ok-coder1
    Apr 16 at 11:38
  • \$\begingroup\$ With IPointerClickHandler, there is no "object clicking". There is a user with a mouse (or touchscreen) doing the clicking. The handler is on the objects the user can click on. \$\endgroup\$
    – Philipp
    Apr 16 at 12:44
  • \$\begingroup\$ I tried the IPointerClickHandler method but it does not work. Code: GitHub Gist \$\endgroup\$
    – ok-coder1
    Apr 16 at 13:00
  • \$\begingroup\$ @ok-coder1 1. Is that script on the objects the player clicks on? 2. do these objects have appropriately shaped colliders? 3. Do you have an Event System in the scene? \$\endgroup\$
    – Philipp
    Apr 16 at 13:22
  • 1
    \$\begingroup\$ @ok-coder1 Sorry, I forgot about one additional thing you need: The Physics Raycaster on the camera. I updated the answer to mention it. \$\endgroup\$
    – Philipp
    Apr 16 at 13:52
1
\$\begingroup\$

ONLY FOR 2D:

If you want to detect the mouse clicking on an object, you could use OnMouseDown and OnMouseUp. OnMouseDown is called when you first press the mouse on the gameobject, OnMouseUp when you release te mouse on top of the gameobject. It does not have to be first pressed on the object.

For these methods to work, you need to add colliders to te gameobject. Then it also doesn't matter if you check IsTrigger or not.

Hope I could help you! CheckerT

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .