Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question physics2D boxcast detect object which out of range

Discussion in '2D' started by reeenyson, Nov 2, 2022.

  1. reeenyson

    reeenyson

    Joined:
    Jun 14, 2022
    Posts:
    4
    Hi, all.
    I use physics2D.boxcast to detect enemy, then I found a phenomenon, that is, when an enemy is instantiated, even if the enemy is out of range, it will be detected once.
    It is just happen when enemy instantiated, after that it is correct.

    Could you please help to see if this phenomenon is normal? or how to avoid?

    Thanks a lot!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Well no, it doesn't.

    Yes, devs posting threads every single day with titles stating that the physics is somehow not working is completely normal. ;) The bitter truth is that 99.9% of the time it's the devs script and how they're using stuff that is wrong.

    It's obviously impossible to answer how to avoid something that isn't broken and you've not posted any other information so you'll need to actually post something be able to determine what's going wrong; scripts, images etc.

    Here's how to post code using code-tags.
     
  3. reeenyson

    reeenyson

    Joined:
    Jun 14, 2022
    Posts:
    4
    Thanks for reply!

    Here is the way how I use Physics2D.BoxCast,
    Code (CSharp):
    1. RaycastHit2D zombie = Physics2D.BoxCast(
    2.         new Vector2(m_FlameThrower.transform.position.x + 1f, m_FlameThrower.transform.position.y + 0.45f),
    3.         Vector2.one, 0, Vector2.right, m_FlameThrowerData.AttackRange, 1 << LayerMask.NameToLayer("Zombie"));
    I draw the BoxCast, when box green, there is no zombie in range; when red, detect enemy.
    upload_2022-11-3_15-57-40.png
    BoxCast will detect new instantiated zombie once,I was confuse about this. upload_2022-11-3_15-59-9.png
     

    Attached Files:

  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Sorry but the above doesn't really clarify anything and isn't clear at all. I just see some green lines which you don't mention and some red vertical lines and a character with a red circle around it. The code snippet doesn't really show anything either unfortunately.

    By far the most overlooked way to debug something is just to verify your expectations in a new empty project i.e. just do a boxcast test to verify you're using it correctly. For someone like me on the forum, there's no way to debug the above for you.

    If you're getting results you don't expect in RaycastHit2D, output those results i.e. what GameObject you hit and check the scene using "Debug.Log()".

    Note:
    Your BoxCast is extremely convoluted, doing things like retrieving transforms twice to add a fixed offset and manually forming a LayerMask by bit-shifting. All this stuff is prone to error.

    Here's how to get a mask from a layer name but you're also best to not use names like this (especially if you ever change them) and just put a public LayerMask field in your script and select it in the inspector then just use this field as the layers arg.

    Also, create a Vector3 field of "Vector3(1f, 0.45f, 0f)" named (say), "zombieOffset" then just do:
    Code (CSharp):
    1.  m_FlameThrower.transform.position + zombieOffset