private void OnDrawGizmos() { Vector2 from = FromAngle(angle) * distance + (Vector2)transform.position; Gizmos.DrawWireSphere(from, 0.2f); try { var corners = ColliderUtils.FindCorners(from, Collider); Gizmos.color = Color.cyan; for (int i = 0; i < corners.Length; i++) { Gizmos.DrawLine(from, corners[i]); } try { var normals = ColliderUtils.FindNormals(corners, Collider); Gizmos.color = Color.white; for (int i = 0; i < normals.Length; i++) { Gizmos.DrawRay(corners[i], normals[i]); } } catch { } } catch { } }
public void Tick(Vector2 lineEndPosition) { RaycastHit2D hit; // Loops until line of sight to player is restored do { // Breaks if end position is inside a collider if (Physics2D.OverlapCircle(lineEndPosition, 0.001f, GroundMask) != null) { break; } Vector2 originPos = currentAnchor.Position; // Raycasts from last anchor to current player position hit = Physics2D.Raycast(originPos, (lineEndPosition - originPos).normalized, Vector2.Distance(originPos, lineEndPosition), GroundMask); // Checks if hitted something and ands anchor in that case if (hit.transform != null) { // Gets previous and current direction Vector2 previousDirection = previousPosition - originPos; Vector2 direction = lineEndPosition - originPos; // Ckecks if hit was clockwise or counter clockwise float angleBetweenRays = Vector2.SignedAngle(previousDirection, direction); bool clockwise = angleBetweenRays < 0; // Gets all corners for given collider and find correct one var corners = ColliderUtils.FindCorners(originPos, hit.collider); var cornerIndex = VectorUtils.FindFirstPointInCircularOrder(previousDirection, originPos, corners, clockwise); var corner = corners[cornerIndex]; Vector2 normal = ColliderUtils.FindNormal(corners[cornerIndex], cornerIndex, hit.collider); // Ads offset corner += normal * EdgeOffset; // Add new anchor to memory previousAnchors.Push(currentAnchor); currentAnchor = new Anchor(corner, normal, clockwise); AnchorAdded?.Invoke(currentAnchor); } } while (hit.transform != null); // Checks if last anchor should be deleted if (previousAnchors.Count > 0) { Anchor previousAnchor = previousAnchors.Peek(); Vector2 lastToCurrentAnchorPos = currentAnchor.Position - previousAnchor.Position; float angle = Vector2.SignedAngle(lastToCurrentAnchorPos, lineEndPosition - currentAnchor.Position); if (currentAnchor.Clockwise == angle > 0) { // Removes anchor from memory currentAnchor = previousAnchors.Pop(); AnchorRemoved?.Invoke(currentAnchor); } } if (DrawDebug) { // Debug Drawing var anchorArray = previousAnchors.ToArray(); for (int i = 0; i < previousAnchors.Count - 1; i++) { Debug.DrawLine(anchorArray[i].Position, anchorArray[i + 1].Position, Color.red); } if (previousAnchors.Count > 0) { Debug.DrawLine(previousAnchors.Peek().Position, currentAnchor.Position, Color.green); } Debug.DrawLine(currentAnchor.Position, lineEndPosition, Color.cyan); // ----------- } // Updates previous position previousPosition = lineEndPosition; }