|
|
|
|
|
|
public Vector3 worldEnd; |
|
|
|
public bool castHit; |
|
|
|
public float hitFraction; |
|
|
|
public float castRadius; |
|
|
|
} |
|
|
|
|
|
|
|
public void Reset() |
|
|
|
|
|
|
/// nothing was hit.
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rayLength"></param>
|
|
|
|
/// <param name="unscaledRayLength"></param>
|
|
|
|
/// <param name="castRadius">Radius of the sphere to use for spherecasting. If 0 or less, rays are used
|
|
|
|
/// <param name="unscaledCastRadius">Radius of the sphere to use for spherecasting. If 0 or less, rays are used
|
|
|
|
/// instead - this may be faster, especially for complex environments.</param>
|
|
|
|
/// <param name="transform">Transform of the GameObject</param>
|
|
|
|
/// <param name="castType">Whether to perform the casts in 2D or 3D.</param>
|
|
|
|
|
|
|
public static void PerceiveStatic(float rayLength, |
|
|
|
public static void PerceiveStatic(float unscaledRayLength, |
|
|
|
float startOffset, float endOffset, float castRadius, |
|
|
|
float startOffset, float endOffset, float unscaledCastRadius, |
|
|
|
Transform transform, CastType castType, float[] perceptionBuffer, |
|
|
|
int layerMask = Physics.DefaultRaycastLayers, |
|
|
|
DebugDisplayInfo debugInfo = null) |
|
|
|
|
|
|
if (castType == CastType.Cast3D) |
|
|
|
{ |
|
|
|
startPositionLocal = new Vector3(0, startOffset, 0); |
|
|
|
endPositionLocal = PolarToCartesian3D(rayLength, angle); |
|
|
|
endPositionLocal = PolarToCartesian3D(unscaledRayLength, angle); |
|
|
|
endPositionLocal.y += endOffset; |
|
|
|
} |
|
|
|
else |
|
|
|
|
|
|
endPositionLocal = PolarToCartesian2D(rayLength, angle); |
|
|
|
endPositionLocal = PolarToCartesian2D(unscaledRayLength, angle); |
|
|
|
} |
|
|
|
|
|
|
|
var startPositionWorld = transform.TransformPoint(startPositionLocal); |
|
|
|
|
|
|
// If there is non-unity scale, |rayDirection| will be different from rayLength.
|
|
|
|
// We want to use this transformed ray length for determining cast length, hit fraction etc.
|
|
|
|
// We also it to scale up or down the sphere or circle radii
|
|
|
|
var scaledRayLength = rayDirection.magnitude; |
|
|
|
// Avoid 0/0 if unscaledRayLength is 0
|
|
|
|
var scaledCastRadius = unscaledRayLength > 0 ? unscaledCastRadius * scaledRayLength / unscaledRayLength : unscaledCastRadius; |
|
|
|
|
|
|
|
// Do the cast and assign the hit information for each detectable object.
|
|
|
|
// sublist[0 ] <- did hit detectableObjects[0]
|
|
|
|
|
|
|
if (castType == CastType.Cast3D) |
|
|
|
{ |
|
|
|
RaycastHit rayHit; |
|
|
|
if (castRadius > 0f) |
|
|
|
if (scaledCastRadius > 0f) |
|
|
|
castHit = Physics.SphereCast(startPositionWorld, castRadius, rayDirection, out rayHit, |
|
|
|
rayLength, layerMask); |
|
|
|
castHit = Physics.SphereCast(startPositionWorld, scaledCastRadius, rayDirection, out rayHit, |
|
|
|
scaledRayLength, layerMask); |
|
|
|
rayLength, layerMask); |
|
|
|
scaledRayLength, layerMask); |
|
|
|
hitFraction = castHit ? rayHit.distance / rayLength : 1.0f; |
|
|
|
// If scaledRayLength is 0, we still could have a hit with sphere casts (maybe?).
|
|
|
|
// To avoid 0/0, set the fraction to 0.
|
|
|
|
hitFraction = castHit ? (scaledRayLength > 0 ? rayHit.distance / scaledRayLength : 0.0f) : 1.0f; |
|
|
|
if (castRadius > 0f) |
|
|
|
if (scaledCastRadius > 0f) |
|
|
|
rayHit = Physics2D.CircleCast(startPositionWorld, castRadius, rayDirection, |
|
|
|
rayLength, layerMask); |
|
|
|
rayHit = Physics2D.CircleCast(startPositionWorld, scaledCastRadius, rayDirection, |
|
|
|
scaledRayLength, layerMask); |
|
|
|
rayHit = Physics2D.Raycast(startPositionWorld, rayDirection, rayLength, layerMask); |
|
|
|
rayHit = Physics2D.Raycast(startPositionWorld, rayDirection, scaledRayLength, layerMask); |
|
|
|
} |
|
|
|
|
|
|
|
castHit = rayHit; |
|
|
|
|
|
|
debugInfo.rayInfos[rayIndex].worldEnd = endPositionWorld; |
|
|
|
debugInfo.rayInfos[rayIndex].castHit = castHit; |
|
|
|
debugInfo.rayInfos[rayIndex].hitFraction = hitFraction; |
|
|
|
debugInfo.rayInfos[rayIndex].castRadius = scaledCastRadius; |
|
|
|
} |
|
|
|
else if (Application.isEditor) |
|
|
|
{ |
|
|
|