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. Dismiss Notice

Question Does Physics2D.CircleCast have an orientation?

Discussion in 'Physics' started by James15478, Jun 13, 2021.

  1. James15478

    James15478

    Joined:
    Apr 2, 2013
    Posts:
    91
    Does the 'circle' of CircleCast have an orientation? For example, if I have a top down game, will the circle "lie down" along the (Unity) Z axis, or if I have a side scroller, will the circle be "upright" along the y-axis?
    Can this orientation be controlled some way?
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,513
    You cannot cast along the Z axis, it's a 2D physics system. Checking if something is at a specific position in 2D space, even if you filter by Z depth is just an overlap test i.e. Physics2D.OverlapCircle etc. There's no cast/sweep through Z.

    All 2D lives on the XY plane.
     
  4. James15478

    James15478

    Joined:
    Apr 2, 2013
    Posts:
    91
    Thanks @MelvMay , that answers my question. So what does the Z-Depth parameter actually do in this circlecast? I'm sorry I don't understand that.
    and thanks also for the reply @Olmi :)
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,513
    It's just a convenient filtering option for results where you can ask that only colliders where the respective Transform position Z is within a range. Know that the physics system knows nothing about Transforms or Z position because it's obviously a 2D physics engine. We just add that utility to fitler the results coming from the physics system for you.

    All the queries also use the ContactFilter2D which has all these filtering options. It's a good place to see those options.

    In the end, 2D physics allows 2D spatial queries on the XY plane. It only has 3 degrees of freedom which are X and Y position and Z rotation. Nothing else exists in the actual physics system. Anything beyond that is just utility we add and in the case of Z position, because Unity is inherently a 3D engine, we provide that post filter option even if the physics system knows nothing about it.

    I will say though that the API does describe this. It says:
    • MinDepth - Only include objects with a Z coordinate (depth) greater than or equal to this value.
    • maxDepth - Only include objects with a Z coordinate (depth) less than or equal to this value.
    So specify the minimum Z and maximum Z depth and everything including and within those limits are returned, everything else is filtered out.
     
  6. James15478

    James15478

    Joined:
    Apr 2, 2013
    Posts:
    91
    @MelvMay got it, thank you very much!
     
    MelvMay likes this.