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): private void MultiRays(BoxCollider2D bc) { RayCaster(new Vector3(bc.bounds.center.x + bc.bounds.extents.x, bc.bounds.center.y + bc.bounds.extents.y, 0)); RayCaster(new Vector3(bc.bounds.center.x + -bc.bounds.extents.x, bc.bounds.center.y + bc.bounds.extents.y, 0)); RayCaster(new Vector3(bc.bounds.center.x + bc.bounds.extents.x, bc.bounds.center.y + -bc.bounds.extents.y, 0)); RayCaster(new Vector3(bc.bounds.center.x + -bc.bounds.extents.x, bc.bounds.center.y + -bc.bounds.extents.y, 0)); } private void RayCaster(Vector3 v) { Vector3 direction1 = new Vector3 (localPos.x - v.x, localPos.y - v.y, localPos.z - v.z); Vector3 direction2 = new Vector3(localPos.x - v.x + 0.001f, localPos.y - v.y + 0.001f, localPos.z - v.z); Vector3 direction3 = new Vector3(localPos.x - v.x - 0.001f, localPos.y - v.y - 0.001f, localPos.z - v.z); //White Line on picture to all corners. Debug.DrawRay(localPos, -direction1, Color.white); RaycastHit2D ray1 = Physics2D.Raycast(localPos, -direction1, 4f, 1 << LayerMask.NameToLayer("Wall")); //Work only with lines in field of view. if (PointToAngle(ray1.point, transform.up) < (maxVisibleAngle / 2) || PointToAngle(ray1.point, transform.up) > (-(maxVisibleAngle / 2) + 360)) { //This check for cornes that are in visible range and not behind other walls. if (ray1.point != Vector2.zero & Vector3.Distance(localPos, ray1.point) + 0.01f > Vector3.Distance(localPos, v)) // This must disable red line. { //Red Line on picture to all visible corners. Debug.DrawLine(localPos, ray1.point, Color.red); vertices.Add(v); RaycastHit2D ray2 = Physics2D.Raycast(localPos, -direction2, 4f, 1 << LayerMask.NameToLayer("Wall")); RaycastHit2D ray3 = Physics2D.Raycast(localPos, -direction3, 4f, 1 << LayerMask.NameToLayer("Wall")); if (ray2.point != Vector2.zero & Vector3.Distance(localPos, ray2.point) - 0.01f > Vector3.Distance(localPos, v)) { //Debug.DrawLine(localPos, ray2.point, Color.yellow); vertices.Add(ray2.point); } if (ray3.point != Vector2.zero & Vector3.Distance(localPos, ray3.point) - 0.01f > Vector3.Distance(localPos, v)) { //Debug.DrawLine(localPos, ray3.point, Color.yellow); vertices.Add(ray3.point); } } } } 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): private float PointToAngle(Vector3 point, Vector3 direction) { float angle = Quaternion.FromToRotation(direction, point - localPos).eulerAngles.z; return angle; }
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 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.