Search Unity

Ray2D missing target position

Discussion in 'Physics' started by SVKsuli, Oct 16, 2018.

  1. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    Hello, im trying to make shadow casting, it already work but problem is that the ray casted to corner of wall sometimes missing. Here is picture how it look like:

    Ok i try to explain it better... I find all walls in range and i create Ray2D to all finded corners of this objects (White lines). When Ray.point is on same position (+- some deviation) like original position of corner, i save this position to List<Vector3> and draw a red Line to it (for visualization). From this List i create mesh.

    Here is important part of code:

    Code (CSharp):
    1. private void MultiRays(BoxCollider2D bc)
    2.     {
    3.         RayCaster(new Vector3(bc.bounds.center.x + bc.bounds.extents.x, bc.bounds.center.y + bc.bounds.extents.y, 0));
    4.         RayCaster(new Vector3(bc.bounds.center.x + -bc.bounds.extents.x, bc.bounds.center.y + bc.bounds.extents.y, 0));
    5.         RayCaster(new Vector3(bc.bounds.center.x + bc.bounds.extents.x, bc.bounds.center.y + -bc.bounds.extents.y, 0));
    6.         RayCaster(new Vector3(bc.bounds.center.x + -bc.bounds.extents.x, bc.bounds.center.y + -bc.bounds.extents.y, 0));
    7.     }
    8.  
    9.     private void RayCaster(Vector3 v)
    10.     {
    11.  
    12.         Vector3 direction1 = new Vector3 (localPos.x - v.x,
    13.                                     localPos.y - v.y,
    14.                                     localPos.z - v.z);
    15.         Vector3 direction2 = new Vector3(localPos.x - v.x + 0.001f,
    16.                                     localPos.y - v.y + 0.001f,
    17.                                     localPos.z - v.z);
    18.         Vector3 direction3 = new Vector3(localPos.x - v.x - 0.001f,
    19.                                     localPos.y - v.y - 0.001f,
    20.                                     localPos.z - v.z);
    21.         //White Line on picture to all corners.
    22.         Debug.DrawRay(localPos, -direction1, Color.white);
    23.  
    24.         RaycastHit2D ray1 = Physics2D.Raycast(localPos, -direction1, 4f, 1 << LayerMask.NameToLayer("Wall"));
    25.  
    26.         //Work only with lines in field of view.
    27.         if (PointToAngle(ray1.point, transform.up) < (maxVisibleAngle / 2) ||
    28.             PointToAngle(ray1.point, transform.up) > (-(maxVisibleAngle / 2) + 360))
    29.         {
    30.             //This check for cornes that are in visible range and not behind other walls.
    31.             if (ray1.point != Vector2.zero &
    32.                 Vector3.Distance(localPos, ray1.point) + 0.01f > Vector3.Distance(localPos, v)) // This must disable red line.
    33.             {
    34.                 //Red Line on picture to all visible corners.
    35.                 Debug.DrawLine(localPos, ray1.point, Color.red);
    36.                 vertices.Add(v);
    37.              
    38.                 RaycastHit2D ray2 = Physics2D.Raycast(localPos, -direction2, 4f, 1 << LayerMask.NameToLayer("Wall"));
    39.                 RaycastHit2D ray3 = Physics2D.Raycast(localPos, -direction3, 4f, 1 << LayerMask.NameToLayer("Wall"));
    40.  
    41.                 if (ray2.point != Vector2.zero &
    42.                 Vector3.Distance(localPos, ray2.point) - 0.01f > Vector3.Distance(localPos, v))
    43.                 {
    44.                     //Debug.DrawLine(localPos, ray2.point, Color.yellow);
    45.                     vertices.Add(ray2.point);
    46.                 }
    47.  
    48.                 if (ray3.point != Vector2.zero &
    49.                     Vector3.Distance(localPos, ray3.point) - 0.01f > Vector3.Distance(localPos, v))
    50.                 {
    51.                     //Debug.DrawLine(localPos, ray3.point, Color.yellow);
    52.                     vertices.Add(ray3.point);
    53.                 }
    54.             }
    55.         }  
    56.     }
    It always happens in outside corners, then it look like it physically missed a corner, and aiming tighten around but because i disable all Rays like this, it just miss.

    I hope its understandable and i did not forget something ...and sorry for mistakes in english.

    EDIT:
    Here is next part of code that i forgot:

    Code (CSharp):
    1. private float PointToAngle(Vector3 point, Vector3 direction)
    2. {
    3.         float angle = Quaternion.FromToRotation(direction, point - localPos).eulerAngles.z;
    4.         return angle;
    5. }
     
    Last edited: Oct 16, 2018
  2. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    Ok so i find out whats wrong... I did not realize that my wall texture have only 4px width and same size have sprite in game, so its maybe too small for physics. When i rework all textures and expand all 10x then it stop happening. Im glad that i still using just 4 really simple textures :D

    Only one think stay unsolved. PointToAngle conversion dont work perfectly because when direction is behind player and point is in front (180° angle) result is wrong. All other angles work good. I try to use other calculations but dont know how to make it work. I need calculate full 360° angles and always from face of player rotation.