You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Mouse Click or Finger tap only on eiher game objects or UI objects

Ben Liu
Ben Liu

When facing the situation that we want the mouse click and finger tap can happened only on either game objects or UI items. Here is how:

using UnityEngine.EventSystems;

private void MouseInput() {
  if (Input.GetButtonDown("Fire1")) {

    if (!EventSystem.current.IsPointerOverGameObject()) {
      Debug.Log(Input.mousePosition);
      Vector3 pz = Camera.main.ScreenToWorldPoint(Input.mousePosition);
      pz.z = 0;

      planetSpawner.ReleasePlanet(pz.x);
    }
  }
}

EventSystem.current.IsPointerOverGameObject() determines whether the current click point in happened on an UI item or not.

Ben Liu

Please feel free to contact me if you have any question. <br> https://t.me/NeedoneApp