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 How to Ellipse shape CircleCast in 2D?

Discussion in 'Physics' started by HexDog, Jun 25, 2023.

  1. HexDog

    HexDog

    Joined:
    Mar 31, 2019
    Posts:
    51
    Hi, I'm trying to do an Ellipse shape RayCast in my isometric game, but I'm end up with regular Circle Cast (the Radius is same every where) even if I give direction property to the function, any idea?
    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if (Input.GetMouseButtonDown(0))
    4.         {
    5.             float radius = 1;
    6.             RaycastHit2D hit2D = Physics2D.CircleCast(transform.position, radius,
    7.             Quaternion.Euler(60,0,0) * new Vector3(-1,0,0));
    8.             if(hit2D.collider != null)
    9.             {
    10.                 Debug.Log(hit2D.collider.name);
    11.             }
    12.         }
    13.     }
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    There isn't an Ellipse primitive shape internally within 2D physics; only Circle, Capsule, Edge and 8-side convex polygon.

    The only way would be to approximate it with a PolygonCollider2D outline (producing many primitive polygons).
     
  3. HexDog

    HexDog

    Joined:
    Mar 31, 2019
    Posts:
    51
    Thanks for the suggestion, but how can I cast over this premaded PolygonCollider? There is Cast and Raycast functions available inside Collider2D, but non of them seem working...
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    They all work fine, don't know what to tell you.
     
  5. HexDog

    HexDog

    Joined:
    Mar 31, 2019
    Posts:
    51
    Then please tell me how to use them, because I'm dont get it. I'm alwayes geting zero, even when there is other element inside the collider. (I'm know the array is empty, but I'm doesn't know it's initial size). There is no method overloaded for List option or any function with out parameter...
    Code (CSharp):
    1. public class AttackController : MonoBehaviour
    2. {
    3.     [SerializeField] private Collider2D coll;
    4.     private void Update()
    5.     {
    6.         if (Input.GetMouseButtonDown(0))
    7.         {
    8.             RaycastHit2D[] results = { };
    9.             coll.Raycast(new Vector2(0, 0), results);
    10.             Debug.Log(results.Length);
    11.         }
    12.     }
    13. }
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    The docs tell you how to use them.

    It's shown there in the above docs. If you're using array, passing an empty array isn't useful as it cannot fill in any results so the method returns 0. All physics methods that return multiple values provide results to an array or list.

    You're also passing a degenerate direction (0,0) which makes no sense. It's not clear what you're trying to do above. This just checks a ray from the current collider position in a specific direction. If you want to check if a point overlaps a collider then you use OverlapPoint.
     
  7. HexDog

    HexDog

    Joined:
    Mar 31, 2019
    Posts:
    51
    I would like to collect the enemies surrounding the player when clicked within the bounded area, but my code does not work at all. Any way , I migth replace this with OnEnter and OnExit collider to collect the surroundings.
    Code (CSharp):
    1.         if (Input.GetMouseButtonDown(0))
    2.         {
    3.             List<RaycastHit2D> results = new List<RaycastHit2D>();
    4.             ContactFilter2D filter = new ContactFilter2D();
    5.             coll.Raycast(new Vector2(0,1), filter, results);
    6.             Debug.Log(results.Count); // Still nothing
    7.         }