private static ClipPlaneVertices GetClipPlaneAt(Vector3 pos) { var clipPlane = new ClipPlaneVertices(); if (Camera.main == null) { return(clipPlane); } var transform = Camera.main.transform; var nearClipPlaneOffset = Camera.main.nearClipPlane; clipPlane.UpperLeft = pos - transform.right * halfPlaneWidth; clipPlane.UpperLeft += transform.up * halfPlaneHeight; clipPlane.UpperLeft += transform.forward * nearClipPlaneOffset; clipPlane.UpperRight = pos + transform.right * halfPlaneWidth; clipPlane.UpperRight += transform.up * halfPlaneHeight; clipPlane.UpperRight += transform.forward * nearClipPlaneOffset; clipPlane.LowerLeft = pos - transform.right * halfPlaneWidth; clipPlane.LowerLeft -= transform.up * halfPlaneHeight; clipPlane.LowerLeft += transform.forward * nearClipPlaneOffset; clipPlane.LowerRight = pos + transform.right * halfPlaneWidth; clipPlane.LowerRight -= transform.up * halfPlaneHeight; clipPlane.LowerRight += transform.forward * nearClipPlaneOffset; return(clipPlane); }
private float CheckCameraClipPlane(Vector3 from, Vector3 to) { var closestDistance = -1f; RaycastHit hitInfo; ClipPlaneVertices clipPlane = GetClipPlaneAt(to); Debug.DrawLine(clipPlane.UpperLeft, clipPlane.UpperRight); Debug.DrawLine(clipPlane.UpperRight, clipPlane.LowerRight); Debug.DrawLine(clipPlane.LowerRight, clipPlane.LowerLeft); Debug.DrawLine(clipPlane.LowerLeft, clipPlane.UpperLeft); Debug.DrawLine(from, to, Color.red); Debug.DrawLine(from - transform.right * halfPlaneWidth + transform.up * halfPlaneHeight, clipPlane.UpperLeft, Color.cyan); Debug.DrawLine(from + transform.right * halfPlaneWidth + transform.up * halfPlaneHeight, clipPlane.UpperRight, Color.cyan); Debug.DrawLine(from - transform.right * halfPlaneWidth - transform.up * halfPlaneHeight, clipPlane.LowerLeft, Color.cyan); Debug.DrawLine(from + transform.right * halfPlaneWidth - transform.up * halfPlaneHeight, clipPlane.LowerRight, Color.cyan); if (Physics.Linecast(from, to, out hitInfo) && hitInfo.collider.tag != PlayerTag) { closestDistance = hitInfo.distance - Camera.main.nearClipPlane; } #region Don't like this. It's ... icky if (Physics.Linecast(from - transform.right * halfPlaneWidth + transform.up * halfPlaneHeight, clipPlane.UpperLeft, out hitInfo) && hitInfo.collider.tag != PlayerTag) { if (hitInfo.distance < closestDistance || closestDistance == -1f) { closestDistance = Vector3.Distance(hitInfo.point + transform.right * halfPlaneWidth - transform.up * halfPlaneHeight, from); } } if (Physics.Linecast(from + transform.right * halfPlaneWidth + transform.up * halfPlaneHeight, clipPlane.UpperLeft, out hitInfo) && hitInfo.collider.tag != PlayerTag) { if (hitInfo.distance < closestDistance || closestDistance == -1f) { closestDistance = Vector3.Distance(hitInfo.point - transform.right * halfPlaneWidth - transform.up * halfPlaneHeight, from); } } if (Physics.Linecast(from - transform.right * halfPlaneWidth - transform.up * halfPlaneHeight, clipPlane.UpperLeft, out hitInfo) && hitInfo.collider.tag != PlayerTag) { if (hitInfo.distance < closestDistance || closestDistance == -1f) { closestDistance = Vector3.Distance(hitInfo.point + transform.right * halfPlaneWidth + transform.up * halfPlaneHeight, from); } } if (Physics.Linecast(from + transform.right * halfPlaneWidth - transform.up * halfPlaneHeight, clipPlane.UpperLeft, out hitInfo) && hitInfo.collider.tag != PlayerTag) { if (hitInfo.distance < closestDistance || closestDistance == -1f) { closestDistance = Vector3.Distance(hitInfo.point - transform.right * halfPlaneWidth + transform.up * halfPlaneHeight, from); } } #endregion return(closestDistance); }