Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    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