Search Unity

Question How can check if a circle and a box collide?

Discussion in '2D' started by unity_B074ACCAA43B7835550E, Feb 25, 2023.

  1. unity_B074ACCAA43B7835550E

    unity_B074ACCAA43B7835550E

    Joined:
    Apr 13, 2022
    Posts:
    6
    this is the code:
    collider.cs
    image_2023-02-25_230336205.png image_2023-02-25_230405946.png
    ^^^ LogicScript.cs
    engine images
    image_2023-02-25_230449916.png
    image_2023-02-25_230515804.png I started today, I'm trying to check if the enemy(the purple box) and the player(small white circle) collide
    I copied the code from a tutorial I used before
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Sounds like you either copied or added a bug! Time to get debugging.

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.

    Finally, remember that PHOTOGRAPHS OF CODE ARE NOT A THING.

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. unity_B074ACCAA43B7835550E

    unity_B074ACCAA43B7835550E

    Joined:
    Apr 13, 2022
    Posts:
    6
    I did use the Debug.Log() method, it looks like the problem is with the detection, I also replaced the OnTriggerEnter2D to OnCollisionEnter2D , I am running it on the unity engine.
    I cannot spot any problem(the code I copied is working , I made it with a tutorial and it works in my other app)
    EDIT: Thank you for the advice, next time I will post it correctly, It is not my first time programming so I know how code works , I also tried a long time ago unity and stopped after 2 weeks and I started again today.
    I wont be able to see it now , I am going to sleep it's 00:15 for me
     
    Last edited: Feb 25, 2023
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    If you don't add a Rigidbody2D then these colliders are implicitly Static (non-moving). Static colliders don't contact other Static colliders because they never move and are often used for level geometry etc.

    It does sound like you're guessing how it works too because you won't get a Trigger callback anyway because neither are triggers. I would suggest you follow some basic physics tutorials to get comfy on the basics.

    Regardless, if you don't want a collision response here between the BoxCollider2D and CircleCollider2D then add a Rigidbody2D to both, set both their BodyTypes to Kinematic and enable useFullKinematicContacts. With this setting on, Unity will calculate contacts between these two colliders and you'll get OnCollisionEnter2D (etc) but there will be no collision response. You'll also be able to use the Rigidbody2D.IsTouching to ask if they are touching.
     
  5. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    Does this mean that objects which have a BoxCollider2D, but no Rigidbody2D, are internally still calculated as if I added a Rigidbody2D and set the body type to Static? I'm asking, because I've seen people say that just having a collider without a Rigidbody would be way more efficient if you don't actually need physics based movement.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    The underlying API can only create physics shapes (what a collider contains) against a physics body (what a rigidbody contains). The Static/Kinematic/Dynamic state is for a Rigidbody2D and implicitly for all its attached physics shapes (colliders).

    When you add a collider with a Rigidbody2D then their physics shapes are created via the body of the Rigidbody2D. Whatever its body type is, is what all those colliders/shapes are.

    If you modify the Transform (bad) position/rotation then we can just move/rotate the Rigidbody2D and none of the colliders need to be touched because their geometry (physics shapes) are defined in local Rigidbody2D space i.e. relative to where the pose of the Rigidbody2D is.

    If you add a collider without a Rigidbody2D then we still have to create its shapes against a body (that's the only way it can be done) so in Unity, we use the hidden Static ground-body that we automatically create when Unity starts. This lives in the world origin with no rotation and is never moved. That body has all the colliders without a Rigidbody2D created against it.

    If you modify the Transform against such an implicitly Static collider then we cannot move the ground body so we have to recreate the collider because its physics shapes are created in the space of the ground-body i.e. its local space is its world space.

    None of the above should matter because you should never be modifying the Transform and you certainly shouldn't be doing so on a Static.

    Both colliders would be created against a Static body, that's it. Yep, you'll have another Static body in the world but they don't actively do anything. Have thousands of them if you want. If you absolutely want to modify the Transform and change the position/rotation of such a collider then having a Rigidbody2D set to Static would be way, way more efficient because as I mentioned above, this would mean the Rigidbody2D (body) pose would be changed only and the collider(s) wouldn't need to be recreated.

    So that's a large post but the short of it is that colliders don't move, only Rigidbody2D do and the colliders are created and live relative to them. An implicitly Static collider is the same but the ground-body doesn't move so it'll need to be recreated.

    Adding a Rigidbody2D doesn't make anything inefficient, it's a fallacy. :)
     
    Zephus likes this.
  7. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    Thank you very much for the detailed explanation. This really helped a lot and cleared up some misconceptions I got from reading too many reddit posts. :)
     
    MelvMay likes this.