Skip to content

Commit 93ff8dc

Browse files
committed
fix(Utilities): allow for trigger colliders to be ignored by casts
The various casts done by scripts such as Body Physics, teleporters and the pointers were all colliding with trigger colliders by default. The CustomRaycast script has now been updated so it can be called and informed to ignore trigger colliders without providing an instantiated CustomRaycast object. Body Physics and the teleporters now ignore trigger colliders by default, whereas the pointers still interact with trigger colliders by default because the UI Canvas generates a trigger collider. If the pointer is required to pass through a trigger collider then the object should be put on the Ignore Raycasts layer.
1 parent 713b47c commit 93ff8dc

5 files changed

Lines changed: 20 additions & 16 deletions

File tree

Assets/VRTK/Scripts/Locomotion/VRTK_HeightAdjustTeleport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected virtual float GetTeleportY(Transform target, Vector3 tipPosition)
6262
Ray ray = new Ray(tipPosition + rayStartPositionOffset, -playArea.up);
6363
RaycastHit rayCollidedWith;
6464
#pragma warning disable 0618
65-
if (target && VRTK_CustomRaycast.Raycast(customRaycast, ray, out rayCollidedWith, layersToIgnore, Mathf.Infinity))
65+
if (target != null && VRTK_CustomRaycast.Raycast(customRaycast, ray, out rayCollidedWith, layersToIgnore, Mathf.Infinity, QueryTriggerInteraction.Ignore))
6666
#pragma warning restore 0618
6767
{
6868
newY = (tipPosition.y - rayCollidedWith.distance) + heightOffset;

Assets/VRTK/Scripts/Presence/VRTK_BodyPhysics.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ protected virtual void CheckLean()
626626

627627
//Determine the current valid floor that the user is standing over
628628
#pragma warning disable 0618
629-
currentValidFloorObject = (VRTK_CustomRaycast.Raycast(customRaycast, standingDownRay, out standingDownRayCollision, layersToIgnore, Mathf.Infinity) ? standingDownRayCollision.collider.gameObject : null);
629+
currentValidFloorObject = (VRTK_CustomRaycast.Raycast(customRaycast, standingDownRay, out standingDownRayCollision, layersToIgnore, Mathf.Infinity, QueryTriggerInteraction.Ignore) ? standingDownRayCollision.collider.gameObject : null);
630630
#pragma warning restore 0618
631631

632632
//Don't bother checking for lean if body collisions are disabled
@@ -647,7 +647,7 @@ protected virtual void CheckLean()
647647
// Cast a ray forward just outside the body collider radius to see if anything is blocking your path
648648
// If nothing is blocking your path and you're currently standing over a valid floor
649649
#pragma warning disable 0618
650-
if (!VRTK_CustomRaycast.Raycast(customRaycast, forwardRay, out forwardRayCollision, layersToIgnore, forwardLength) && currentValidFloorObject != null)
650+
if (!VRTK_CustomRaycast.Raycast(customRaycast, forwardRay, out forwardRayCollision, layersToIgnore, forwardLength, QueryTriggerInteraction.Ignore) && currentValidFloorObject != null)
651651
#pragma warning restore 0618
652652
{
653653
CalculateLean(standingDownRayStartPosition, forwardLength, standingDownRayCollision.distance);
@@ -668,7 +668,7 @@ protected virtual void CalculateLean(Vector3 startPosition, float forwardLength,
668668

669669
//Cast a ray down from the end of the forward ray position
670670
#pragma warning disable 0618
671-
if (VRTK_CustomRaycast.Raycast(customRaycast, downRay, out downRayCollision, layersToIgnore, Mathf.Infinity))
671+
if (VRTK_CustomRaycast.Raycast(customRaycast, downRay, out downRayCollision, layersToIgnore, Mathf.Infinity, QueryTriggerInteraction.Ignore))
672672
#pragma warning restore 0618
673673
{
674674
//Determine the difference between the original down ray and the projected forward a bit downray
@@ -1034,7 +1034,7 @@ protected virtual float ControllerHeightCheck(GameObject controllerObj)
10341034
Ray ray = new Ray(controllerObj.transform.position, -playArea.up);
10351035
RaycastHit rayCollidedWith;
10361036
#pragma warning disable 0618
1037-
VRTK_CustomRaycast.Raycast(customRaycast, ray, out rayCollidedWith, layersToIgnore, Mathf.Infinity);
1037+
VRTK_CustomRaycast.Raycast(customRaycast, ray, out rayCollidedWith, layersToIgnore, Mathf.Infinity, QueryTriggerInteraction.Ignore);
10381038
#pragma warning restore 0618
10391039
return controllerObj.transform.position.y - rayCollidedWith.distance;
10401040
}
@@ -1078,7 +1078,7 @@ protected virtual void SnapToNearestFloor()
10781078
Ray ray = new Ray(headset.transform.position, -playArea.up);
10791079
RaycastHit rayCollidedWith;
10801080
#pragma warning disable 0618
1081-
bool rayHit = VRTK_CustomRaycast.Raycast(customRaycast, ray, out rayCollidedWith, layersToIgnore, Mathf.Infinity);
1081+
bool rayHit = VRTK_CustomRaycast.Raycast(customRaycast, ray, out rayCollidedWith, layersToIgnore, Mathf.Infinity, QueryTriggerInteraction.Ignore);
10821082
#pragma warning restore 0618
10831083
hitFloorYDelta = playArea.position.y - rayCollidedWith.point.y;
10841084

Assets/VRTK/Scripts/Presence/VRTK_HeadsetControllerAware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ protected virtual void RayCastToController(GameObject controller, Transform cust
204204
{
205205
var destination = (customDestination ? customDestination.position : controller.transform.position);
206206
RaycastHit hitInfo;
207-
if (VRTK_CustomRaycast.Linecast(customRaycast, headset.position, destination, out hitInfo, new LayerMask()))
207+
if (VRTK_CustomRaycast.Linecast(customRaycast, headset.position, destination, out hitInfo, new LayerMask(), QueryTriggerInteraction.Ignore))
208208
{
209209
obscured = true;
210210
}

Assets/VRTK/Scripts/Utilities/VRTK_CustomRaycast.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ public class VRTK_CustomRaycast : MonoBehaviour
2727
/// <param name="hitData">The raycast hit data.</param>
2828
/// <param name="ignoreLayers">A layermask of layers to ignore from the raycast.</param>
2929
/// <param name="length">The maximum length of the raycast.</param>
30+
/// <param name="affectTriggers">Determines the trigger interaction level of the cast.</param>
3031
/// <returns>Returns true if the raycast successfully collides with a valid object.</returns>
31-
public static bool Raycast(VRTK_CustomRaycast customCast, Ray ray, out RaycastHit hitData, LayerMask ignoreLayers, float length = Mathf.Infinity)
32+
public static bool Raycast(VRTK_CustomRaycast customCast, Ray ray, out RaycastHit hitData, LayerMask ignoreLayers, float length = Mathf.Infinity, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal)
3233
{
3334
if (customCast != null)
3435
{
3536
return customCast.CustomRaycast(ray, out hitData, length);
3637
}
3738
else
3839
{
39-
return Physics.Raycast(ray, out hitData, length, ~ignoreLayers);
40+
return Physics.Raycast(ray, out hitData, length, ~ignoreLayers, affectTriggers);
4041
}
4142
}
4243

@@ -48,16 +49,17 @@ public static bool Raycast(VRTK_CustomRaycast customCast, Ray ray, out RaycastHi
4849
/// <param name="endPosition">The world position to end the linecast at.</param>
4950
/// <param name="hitData">The linecast hit data.</param>
5051
/// <param name="ignoreLayers">A layermask of layers to ignore from the linecast.</param>
52+
/// <param name="affectTriggers">Determines the trigger interaction level of the cast.</param>
5153
/// <returns>Returns true if the linecast successfully collides with a valid object.</returns>
52-
public static bool Linecast(VRTK_CustomRaycast customCast, Vector3 startPosition, Vector3 endPosition, out RaycastHit hitData, LayerMask ignoreLayers)
54+
public static bool Linecast(VRTK_CustomRaycast customCast, Vector3 startPosition, Vector3 endPosition, out RaycastHit hitData, LayerMask ignoreLayers, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal)
5355
{
5456
if (customCast != null)
5557
{
5658
return customCast.CustomLinecast(startPosition, endPosition, out hitData);
5759
}
5860
else
5961
{
60-
return Physics.Linecast(startPosition, endPosition, out hitData);
62+
return Physics.Linecast(startPosition, endPosition, out hitData, ~ignoreLayers, affectTriggers);
6163
}
6264
}
6365

@@ -82,7 +84,7 @@ public virtual bool CustomRaycast(Ray ray, out RaycastHit hitData, float length
8284
/// <returns>Returns true if the line successfully collides with a valid object.</returns>
8385
public virtual bool CustomLinecast(Vector3 startPosition, Vector3 endPosition, out RaycastHit hitData)
8486
{
85-
return Physics.Linecast(startPosition, endPosition, out hitData, ~layersToIgnore);
87+
return Physics.Linecast(startPosition, endPosition, out hitData, ~layersToIgnore, triggerInteraction);
8688
}
8789
}
8890
}

DOCUMENTATION.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6692,31 +6692,33 @@ For example, the VRTK_BodyPhysics script can be set to ignore trigger colliders
66926692

66936693
### Class Methods
66946694

6695-
#### Raycast/5
6695+
#### Raycast/6
66966696

6697-
> `public static bool Raycast(VRTK_CustomRaycast customCast, Ray ray, out RaycastHit hitData, LayerMask ignoreLayers, float length = Mathf.Infinity)`
6697+
> `public static bool Raycast(VRTK_CustomRaycast customCast, Ray ray, out RaycastHit hitData, LayerMask ignoreLayers, float length = Mathf.Infinity, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal)`
66986698
66996699
* Parameters
67006700
* `VRTK_CustomRaycast customCast` - The optional object with customised cast parameters.
67016701
* `Ray ray` - The Ray to cast with.
67026702
* `out RaycastHit hitData` - The raycast hit data.
67036703
* `LayerMask ignoreLayers` - A layermask of layers to ignore from the raycast.
67046704
* `float length` - The maximum length of the raycast.
6705+
* `QueryTriggerInteraction affectTriggers` - Determines the trigger interaction level of the cast.
67056706
* Returns
67066707
* `bool` - Returns true if the raycast successfully collides with a valid object.
67076708

67086709
The Raycast method is used to generate a raycast either from the given CustomRaycast object or a default Physics.Raycast.
67096710

6710-
#### Linecast/5
6711+
#### Linecast/6
67116712

6712-
> `public static bool Linecast(VRTK_CustomRaycast customCast, Vector3 startPosition, Vector3 endPosition, out RaycastHit hitData, LayerMask ignoreLayers)`
6713+
> `public static bool Linecast(VRTK_CustomRaycast customCast, Vector3 startPosition, Vector3 endPosition, out RaycastHit hitData, LayerMask ignoreLayers, QueryTriggerInteraction affectTriggers = QueryTriggerInteraction.UseGlobal)`
67136714
67146715
* Parameters
67156716
* `VRTK_CustomRaycast customCast` - The optional object with customised cast parameters.
67166717
* `Vector3 startPosition` - The world position to start the linecast from.
67176718
* `Vector3 endPosition` - The world position to end the linecast at.
67186719
* `out RaycastHit hitData` - The linecast hit data.
67196720
* `LayerMask ignoreLayers` - A layermask of layers to ignore from the linecast.
6721+
* `QueryTriggerInteraction affectTriggers` - Determines the trigger interaction level of the cast.
67206722
* Returns
67216723
* `bool` - Returns true if the linecast successfully collides with a valid object.
67226724

0 commit comments

Comments
 (0)