// Custom Raycast|Utilities|90080
namespace VRTK
{
using UnityEngine;
///
/// A Custom Raycast allows to specify custom options for a Physics.Raycast.
///
///
/// A number of other scripts can utilise a Custom Raycast to further customise the raycasts that the scripts use internally.
///
/// For example, the VRTK_BodyPhysics script can be set to ignore trigger colliders when casting to see if it should teleport up or down to the nearest floor.
///
[AddComponentMenu("VRTK/Scripts/Utilities/VRTK_CustomRaycast")]
public class VRTK_CustomRaycast : MonoBehaviour
{
[Tooltip("The layers to ignore when raycasting.")]
public LayerMask layersToIgnore = Physics.IgnoreRaycastLayer;
[Tooltip("Determines whether the ray will interact with trigger colliders.")]
public QueryTriggerInteraction triggerInteraction = QueryTriggerInteraction.UseGlobal;
///
/// The Raycast method is used to generate a raycast either from the given CustomRaycast object or a default Physics.Raycast.
///
/// The optional object with customised cast parameters.
/// The Ray to cast with.
/// The raycast hit data.
/// A layermask of layers to ignore from the raycast.
/// The maximum length of the raycast.
/// Determines the trigger interaction level of the cast.
/// Returns true if the raycast successfully collides with a valid object.
public static bool Raycast(VRTK_CustomRaycast customCast, Ray ray, out RaycastHit hitData, LayerMask ignoreLayers, float length = Mathf.Infinity, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal)
{
if (customCast != null)
{
return customCast.CustomRaycast(ray, out hitData, length);
}
else
{
return Physics.Raycast(ray, out hitData, length, ~ignoreLayers, affectTriggers);
}
}
///
/// The Linecast method is used to generate a linecast either from the given CustomRaycast object or a default Physics.Linecast.
///
/// The optional object with customised cast parameters.
/// The world position to start the linecast from.
/// The world position to end the linecast at.
/// The linecast hit data.
/// A layermask of layers to ignore from the linecast.
/// Determines the trigger interaction level of the cast.
/// Returns true if the linecast successfully collides with a valid object.
public static bool Linecast(VRTK_CustomRaycast 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);
}
}
///
/// The CapsuleCast method is used to generate a capsulecast either from the given CustomRaycast object or a default Physics.CapsuleCast.
///
/// The optional object with customised cast parameters.
/// The center of the sphere at the start of the capsule.
/// The center of the sphere at the end of the capsule.
/// The radius of the capsule.
/// The direction into which to sweep the capsule.
/// The max length of the sweep.
/// The capsulecast hit data.
/// A layermask of layers to ignore from the capsulecast.
/// Determines the trigger interaction level of the cast.
/// Returns true if the capsulecast successfully collides with a valid object.
public static bool CapsuleCast(VRTK_CustomRaycast customCast, Vector3 point1, Vector3 point2, float radius, Vector3 direction, float maxDistance, out RaycastHit hitData, LayerMask ignoreLayers, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal)
{
if (customCast != null)
{
return customCast.CustomCapsuleCast(point1, point2, radius, direction, maxDistance, out hitData);
}
else
{
return Physics.CapsuleCast(point1, point2, radius, direction, out hitData, maxDistance, ~ignoreLayers, affectTriggers);
}
}
///
/// The BoxCast method is used to generate a boxcast either from the given CustomRaycast object or a default Physics.BoxCast.
///
/// The optional object with customised cast parameters.
/// The center of the box.
/// Half the size of the box in each dimension.
/// The direction in which to cast the box.
/// The rotation of the box.
/// The max length of the cast.
/// The boxcast hit data.
/// A layermask of layers to ignore from the boxcast.
/// Determines the trigger interaction level of the cast.
/// Returns true if the boxcast successfully collides with a valid object.
public static bool BoxCast(VRTK_CustomRaycast customCast, Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation, float maxDistance, out RaycastHit hitData, LayerMask ignoreLayers, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal)
{
if (customCast != null)
{
return customCast.CustomBoxCast(center, halfExtents, direction, orientation, maxDistance, out hitData);
}
else
{
return Physics.BoxCast(center, halfExtents, direction, out hitData, orientation, maxDistance, ~ignoreLayers, affectTriggers);
}
}
///
/// The CustomRaycast method is used to generate a raycast based on the options defined in the CustomRaycast object.
///
/// The Ray to cast with.
/// The raycast hit data.
/// The maximum length of the raycast.
/// Returns true if the raycast successfully collides with a valid object.
public virtual bool CustomRaycast(Ray ray, out RaycastHit hitData, float length = Mathf.Infinity)
{
return Physics.Raycast(ray, out hitData, length, ~layersToIgnore, triggerInteraction);
}
///
/// The CustomLinecast method is used to generate a linecast based on the options defined in the CustomRaycast object.
///
/// The world position to start the linecast from.
/// The world position to end the linecast at.
/// The linecast hit data.
/// Returns true if the line successfully collides with a valid object.
public virtual bool CustomLinecast(Vector3 startPosition, Vector3 endPosition, out RaycastHit hitData)
{
return Physics.Linecast(startPosition, endPosition, out hitData, ~layersToIgnore, triggerInteraction);
}
///
/// The CustomCapsuleCast method is used to generate a capsulecast based on the options defined in the CustomRaycast object.
///
/// The center of the sphere at the start of the capsule.
/// The center of the sphere at the end of the capsule.
/// The radius of the capsule.
/// The direction into which to sweep the capsule.
/// The max length of the sweep.
/// The capsulecast hit data.
/// Returns true if the capsule successfully collides with a valid object.
public virtual bool CustomCapsuleCast(Vector3 point1, Vector3 point2, float radius, Vector3 direction, float maxDistance, out RaycastHit hitData)
{
return Physics.CapsuleCast(point1, point2, radius, direction, out hitData, maxDistance, ~layersToIgnore, triggerInteraction);
}
///
/// The CustomBoxCast method is used to generate a boxcast based on the options defined in the CustomRaycast object.
///
/// The center of the box.
/// Half the size of the box in each dimension.
/// The direction in which to cast the box.
/// The rotation of the box.
/// The max length of the cast.
/// The boxcast hit data.
/// Returns true if the box successfully collides with a valid object.
public virtual bool CustomBoxCast(Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation, float maxDistance, out RaycastHit hitData)
{
return Physics.BoxCast(center, halfExtents, direction, out hitData, orientation, maxDistance, ~layersToIgnore, triggerInteraction);
}
}
}