Search Unity

2D Hitboxes

Discussion in '2D' started by GeekyHuff, Jan 23, 2019.

  1. GeekyHuff

    GeekyHuff

    Joined:
    Jul 10, 2014
    Posts:
    6
    Hello, I've been working on this simple 2D clicking game. But basically the objects randomly don't register like, that's just how it feels when clicking them. It may be just part of the game engine or something. But I was wondering if anyone could shed some light on this. I have attached a couple pictures, One is just how the game looks when playing. Second is collider & rigid body information, I've made the collider a lot bigger than the actual object and it sort of feels better. But basically what I'm asking is that does anyone know of a way I can make it play better?

    Also here is my object destroy script, it is very basic.

    Code (CSharp):
    1. public class DestroyOnBubble : MonoBehaviour
    2. {
    3.     public void OnMouseDown()
    4.     {
    5.         Destroy(gameObject);
    6.         TimeLeft.timeLeft += 1f;
    7.         ScoreScript.scoreValue += 100;
    8.  
    9.         if (ScoreScript.scoreValue == 1000)
    10.         {
    11.             SceneManager.LoadScene(2);
    12.             ScoreScript.scoreValue = 0;
    13.             TimeLeft.timeLeft = 15;
    14.         }
    15.     }
    16. }
    Ignore the Scorescript stuff. Game.png Collider.png
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    you're probably missing some raycasters or have some your shouldn't. add a raycast component to your bubbles should help. see if you can find areas where your clicks are not registering correctly (such as near the overlay text or other invisible objects) and ensure you remove raycasting from them (canvasgroup component with interactable false). otherwise you might need to explain in more detail what exactly doesnt work (randomly isn't exactly fixable)
     
  3. GeekyHuff

    GeekyHuff

    Joined:
    Jul 10, 2014
    Posts:
    6
    Thanks for the reply, I not using any raycast at all. I've added a raycast to the bubble and it seems better. But there is defiantly still hit reg problems. Let me try to explain the problem more clearly. Basically when playing throughout a level, when you destroy a bubble sometimes to seems to work perfectly fine. But it seems to when you try to go really fast and spam click the objects it doesn't seem to register them getting clicked that well. Here's the raycast I'm using.

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if (Input.GetMouseButtonDown(0))
    4.         {
    5.             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    6.  
    7.             if (hit.collider != null)
    8.             {
    9.                 if (hit.collider.gameObject == gameObject) Destroy(gameObject);
    10.             }
    11.         }
    12.     }
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Are you sure there's not more than a single collider where you click. Physics queries have no concept of rendering depth i.e. they won't give you the collider that you perceive as being closer due to some other component rendering that way. You can use queries that return multiple results and check each result if that's perhaps the case here. Either that or use the LayerMask to only select specific layers.

    Also note that when checking if a collider is over a point, you really shouldn't be using raycast although it's use a lot by users, probably because it's used widely in 3D physics. Anyway, you should consider using the method designed for this which is OverlapPoint.

    You probably know this but GetMouseButtonDown is only true if you clicked it that frame. I presume this is what you want.

    That's all I've got for now. :)