/// <summary> /// Generates a SphereCast either from the given <see cref="PhysicsCast"/> object or a default <see cref="Physics.SphereCast"/>. /// </summary> /// <param name="customCast">The optional <see cref="PhysicsCast"/> with customized cast parameters.</param> /// <param name="origin">The origin point of the sphere to cast.</param> /// <param name="radius">The radius of the sphere.</param> /// <param name="direction">The direction into which to sweep the sphere.</param> /// <param name="hitData">The <see cref="RaycastHit"/> data.</param> /// <param name="maxDistance">The max length of the sweep.</param> /// <param name="ignoreLayers">A <see cref="LayerMask"/> of layers to ignore from the SphereCast.</param> /// <param name="affectTriggers">Determines the trigger interaction level of the cast.</param> /// <returns><see langword="true"/> if the SphereCast successfully collides with a valid <see cref="GameObject"/>.</returns> public static bool SphereCast(PhysicsCast customCast, Vector3 origin, float radius, Vector3 direction, out RaycastHit hitData, float maxDistance, LayerMask ignoreLayers, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal) { if (customCast != null) { return(customCast.CustomSphereCast(origin, radius, direction, out hitData, maxDistance)); } else { return(Physics.SphereCast(origin, radius, direction, out hitData, maxDistance, ~ignoreLayers, affectTriggers)); } }
/// <summary> /// Generates the points for the cast. /// </summary> protected virtual void GeneratePoints() { Ray ray = new Ray(origin.transform.position, origin.transform.forward); RaycastHit hitData; bool hasCollided = PhysicsCast.Raycast(physicsCast, ray, out hitData, maximumLength, Physics.IgnoreRaycastLayer); TargetHit = (hasCollided ? hitData : (RaycastHit?)null); points[0] = origin.transform.position; points[1] = (hasCollided ? hitData.point : origin.transform.position + origin.transform.forward * maximumLength); }
/// <summary> /// Generates a Linecast either from the given <see cref="PhysicsCast"/> object or a default <see cref="Physics.Linecast"/>. /// </summary> /// <param name="customCast">The optional <see cref="PhysicsCast"/> with customized cast parameters.</param> /// <param name="startPosition">The world position to start the Linecast from.</param> /// <param name="endPosition">The world position to end the Linecast at.</param> /// <param name="hitData">The <see cref="RaycastHit"/> data.</param> /// <param name="ignoreLayers">A <see cref="LayerMask"/> of layers to ignore from the Linecast.</param> /// <param name="affectTriggers">Determines the trigger interaction level of the cast.</param> /// <returns><see langword="true"/> if the Linecast successfully collides with a valid <see cref="GameObject"/>.</returns> public static bool Linecast(PhysicsCast customCast, Vector3 startPosition, Vector3 endPosition, out RaycastHit hitData, LayerMask ignoreLayers, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal) { if (customCast != null) { return(customCast.CustomLinecast(startPosition, endPosition, out hitData)); } else { return(Physics.Linecast(startPosition, endPosition, out hitData, ~ignoreLayers, affectTriggers)); } }
/// <summary> /// Generates a RaycastAll either from the given <see cref="PhysicsCast"/> object or a default <see cref="Physics.Raycast"/>. /// </summary> /// <param name="customCast">The optional <see cref="PhysicsCast"/> with customized cast parameters.</param> /// <param name="ray">The <see cref="Ray"/> to cast with.</param> /// <param name="length">The maximum length of the <see cref="Ray"/>.</param> /// <param name="ignoreLayers">A <see cref="LayerMask"/> of layers to ignore from the <see cref="Ray"/>.</param> /// <param name="affectTriggers">Determines the trigger interaction level of the <see cref="Ray"/>.</param> /// <returns>A collection of collisions determined by the cast.</returns> public static RaycastHit[] RaycastAll(PhysicsCast customCast, Ray ray, float length, LayerMask ignoreLayers, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal) { if (customCast != null) { return(customCast.CustomRaycastAll(ray, length)); } else { return(Physics.RaycastAll(ray, length, ~ignoreLayers, affectTriggers)); } }
/// <summary> /// Generates a BoxCastAll either from the given <see cref="PhysicsCast"/> object or a default <see cref="Physics.BoxCast"/>. /// </summary> /// <param name="customCast">The optional <see cref="PhysicsCast"/> with customized cast parameters.</param> /// <param name="center">The center of the box.</param> /// <param name="halfExtents">Half the size of the box in each dimension.</param> /// <param name="direction">The direction in which to cast the box.</param> /// <param name="orientation">The rotation of the box.</param> /// <param name="maxDistance">The max length of the cast.</param> /// <param name="ignoreLayers">A <see cref="LayerMask"/> of layers to ignore from the BoxCast.</param> /// <param name="affectTriggers">Determines the trigger interaction level of the cast.</param> /// <returns>A collection of collisions determined by the cast.</returns> public static RaycastHit[] BoxCastAll(PhysicsCast customCast, Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation, float maxDistance, LayerMask ignoreLayers, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal) { if (customCast != null) { return(customCast.CustomBoxCastAll(center, halfExtents, direction, orientation, maxDistance)); } else { return(Physics.BoxCastAll(center, halfExtents, direction, orientation, maxDistance, ~ignoreLayers, affectTriggers)); } }
/// <summary> /// Generates a CapsuleCastAll either from the given <see cref="PhysicsCast"/> object or a default <see cref="Physics.CapsuleCast"/>. /// </summary> /// <param name="customCast">The optional <see cref="PhysicsCast"/> with customized cast parameters.</param> /// <param name="point1">The center of the sphere at the start of the capsule.</param> /// <param name="point2">The center of the sphere at the end of the capsule.</param> /// <param name="radius">The radius of the capsule.</param> /// <param name="direction">The direction into which to sweep the capsule.</param> /// <param name="maxDistance">The max length of the sweep.</param> /// <param name="ignoreLayers">A <see cref="LayerMask"/> of layers to ignore from the CapsuleCast.</param> /// <param name="affectTriggers">Determines the trigger interaction level of the cast.</param> /// <returns>A collection of collisions determined by the cast.</returns> public static RaycastHit[] CapsuleCastAll(PhysicsCast customCast, Vector3 point1, Vector3 point2, float radius, Vector3 direction, float maxDistance, LayerMask ignoreLayers, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal) { if (customCast != null) { return(customCast.CustomCapsuleCastAll(point1, point2, radius, direction, maxDistance)); } else { return(Physics.CapsuleCastAll(point1, point2, radius, direction, maxDistance, ~ignoreLayers, affectTriggers)); } }
/// <inheritdoc /> public override void CastPoints() { if (!isActiveAndEnabled) { return; } Ray ray = new Ray(transform.position, transform.forward); RaycastHit hitData; bool hasCollided = PhysicsCast.Raycast(physicsCast, ray, out hitData, maximumLength, Physics.IgnoreRaycastLayer); TargetHit = (hasCollided ? hitData : (RaycastHit?)null); points[0] = transform.position; points[1] = (hasCollided ? hitData.point : transform.position + transform.forward * maximumLength); ResultsChanged?.Invoke(eventData.Set(TargetHit, Points)); }
/// <summary> /// Checks for collisions along the parabolic line segments and generates the final points. /// </summary> /// <param name="forward">The forward direction to use for the checks.</param> /// <param name="down">The downwards direction to use for the checks.</param> protected virtual void GeneratePointsIncludingSegments(Vector3 forward, Vector3 down) { GeneratePoints(forward, down); collisionCheckFrequency = Mathf.Clamp(collisionCheckFrequency, 0, segmentCount); int step = segmentCount / (collisionCheckFrequency > 0 ? collisionCheckFrequency : 1); for (int index = 0; index < segmentCount - step; index += step) { Vector3 currentPoint = points[index]; Vector3 nextPoint = index + step < points.Count ? points[index + step] : points[points.Count - 1]; Vector3 nextPointDirection = (nextPoint - currentPoint).normalized; float nextPointDistance = Vector3.Distance(currentPoint, nextPoint); Ray pointsRay = new Ray(currentPoint, nextPointDirection); RaycastHit pointsHitData; if (!PhysicsCast.Raycast(physicsCast, pointsRay, out pointsHitData, nextPointDistance, Physics.IgnoreRaycastLayer)) { continue; } Vector3 collisionPoint = pointsRay.GetPoint(pointsHitData.distance); Ray downwardRay = new Ray(collisionPoint + Vector3.up * 0.01f, Vector3.down); RaycastHit downwardHitData; if (!PhysicsCast.Raycast(physicsCast, downwardRay, out downwardHitData, float.PositiveInfinity, Physics.IgnoreRaycastLayer)) { continue; } TargetHit = downwardHitData; Vector3 newDownPosition = downwardRay.GetPoint(downwardHitData.distance); Vector3 newJointPosition = newDownPosition.y < forward.y ? new Vector3(newDownPosition.x, forward.y, newDownPosition.z) : forward; GeneratePoints(newJointPosition, newDownPosition); break; } ResultsChanged?.Invoke(eventData.Set(TargetHit, Points)); }
/// <summary> /// Projects a straight line downwards from the provided point. /// </summary> /// <param name="downwardOrigin">The origin of the projected line.</param> /// <returns>The collision point or the point being the furthest away on the cast line if nothing is hit.</returns> protected virtual Vector3 ProjectDown(Vector3 downwardOrigin) { Vector3 point = Vector3.zero; Ray ray = new Ray(downwardOrigin, Vector3.down); RaycastHit hitData; bool downRayHit = PhysicsCast.Raycast(physicsCast, ray, out hitData, maximumLength.y, Physics.IgnoreRaycastLayer); if (!downRayHit || (TargetHit?.collider != null && TargetHit.Value.collider != hitData.collider)) { TargetHit = null; point = ray.GetPoint(0f); } if (downRayHit) { point = ray.GetPoint(hitData.distance); TargetHit = hitData; } return(point); }
/// <summary> /// Projects a straight line forward from the current origin. /// </summary> /// <returns>The collision point or the point being the furthest away on the cast line if nothing is hit.</returns> protected virtual Vector3 ProjectForward() { float rotation = Vector3.Dot(Vector3.up, origin.transform.forward.normalized); float length = maximumLength.x; if ((rotation * 100f) > heightLimitAngle) { float controllerRotationOffset = 1f - (rotation - heightLimitAngle / 100f); length = maximumLength.x * controllerRotationOffset * controllerRotationOffset; } Ray ray = new Ray(origin.transform.position, origin.transform.forward); RaycastHit hitData; bool hasCollided = PhysicsCast.Raycast(physicsCast, ray, out hitData, length, Physics.IgnoreRaycastLayer); // Adjust the cast length if something is blocking it. if (hasCollided && hitData.distance < length) { length = hitData.distance; } // Use an offset to move the point back and up a bit to prevent the cast clipping at the collision point. return(ray.GetPoint(length - ADJUSTMENT_OFFSET) + (Vector3.up * ADJUSTMENT_OFFSET)); }