Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Some problem relate to Boxcast2D

Discussion in '2D' started by dreamerPR, Mar 19, 2023.

  1. dreamerPR

    dreamerPR

    Joined:
    Nov 13, 2018
    Posts:
    1
    I understand the distance is for define the distance of Boxcast that mean the one that use to define the direction is dir which is Vector2.down in this case. The problem is sometime when the character is near a wall. results give a hit.point that is on the opposite side of direction. The only case that is possible is because size.y of the boxcast. I think the hit.point is too far in this case tho.
    here the code
    Code (CSharp):
    1. Vector2 size = new Vector2(colliderSize.x, 0.001f);
    2.         Vector2 offset = new Vector2(0,  colliderSize.y * 0.5f);
    3.         Vector3 offset3 = offset;
    4.         Vector3 start = boxCollider2D.bounds.center - offset3;
    5.         ExtDebug.DrawBoxCastBox(start, size, Quaternion.identity, Vector2.down, Vector2.Distance(start, des), Color.red);
    6.         RaycastHit2D[] results = Physics2D.BoxCastAll(start, size, 0, Vector2.down, Vector2.Distance(start, des), LayerMask.GetMask(TagAndLayer.tileMapLayer));
    image of the problem

    the axis show the position of hit.point. A red box show Boxcast2D area.


    unit y is start.y. hit y is hit.point.y