VRTK_BodyPhysics
extends VRTK_DestinationMarker
Overview
The body physics script deals with how a user's body in the scene reacts to world physics and how to handle drops.
The body physics creates a rigidbody and collider for where the user is standing to allow physics interactions and prevent walking through walls.
Upon actually moving in the play area, the rigidbody is set to kinematic to prevent the world from being pushed back in the user's view reducing sickness.
The body physics script also deals with snapping a user to the nearest floor if they look over a ledge or walk up stairs then it will move the play area to simulate movement in the scene.
To allow for peeking over a ledge and not falling, a fall restiction can happen by keeping a controller over the existing floor and the snap to the nearest floor will not happen until the controllers are also over the floor.
Inspector Parameters
- Enable Body Collisions: If checked then the body collider and rigidbody will be used to check for rigidbody collisions.
- Ignore Grabbed Collisions: If this is checked then any items that are grabbed with the controller will not collide with the body collider. This is very useful if the user is required to grab and wield objects because if the collider was active they would bounce off the collider.
- Headset Y Offset: The collider which is created for the user is set at a height from the user's headset position. If the collider is required to be lower to allow for room between the play area collider and the headset then this offset value will shorten the height of the generated collider.
- Movement Threshold: The amount of movement of the headset between the headset's current position and the current standing position to determine if the user is walking in play space and to ignore the body physics collisions if the movement delta is above this threshold.
- Standing History Samples: The maximum number of samples to collect of headset position before determining if the current standing position within the play space has changed.
- Lean Y Threshold: The
y
distance between the headset and the object being leaned over, if object being leaned over is taller than this threshold then the current standing position won't be updated. - Layers To Ignore: The layers to ignore when raycasting to find floors.
- Fall Restriction: A check to see if the drop to nearest floor should take place. If the selected restrictor is still over the current floor then the drop to nearest floor will not occur. Works well for being able to lean over ledges and look down. Only works for falling down not teleporting up.
- Gravity Fall Y Threshold: When the
y
distance between the floor and the headset exceeds this distance andEnable Body Collisions
is true then the rigidbody gravity will be used instead of teleport to drop to nearest floor. - Blink Y Threshold: The
y
distance between the floor and the headset that must change before a fade transition is initiated. If the new user location is at a higher distance than the threshold then the headset blink transition will activate on teleport. If the new user location is within the threshold then no blink transition will happen, which is useful for walking up slopes, meshes and terrains to prevent constant blinking. - Floor Height Tolerance: The amount the
y
position needs to change by between the current floory
position and the previous floory
position before a change in floor height is considered to have occurred. A higher value here will mean that aDrop To Floor
will be less likely to happen if they
of the floor beneath the user hasn't changed as much as the given threshold.
Class Variables
public enum FallingRestrictors
- Options for testing if a play space fall is validNo_Restriction
- Always drop to nearest floor when the headset is no longer over the current standing object.Left_Controller
- Don't drop to nearest floor if the Left Controller is still over the current standing object even if the headset isn't.Right_Controller
- Don't drop to nearest floor if the Right Controller is still over the current standing object even if the headset isn't.Either_Controller
- Don't drop to nearest floor if Either Controller is still over the current standing object even if the headset isn't.Both_Controllers
- Don't drop to nearest floor only if Both Controllers are still over the current standing object even if the headset isn't.
Class Events
StartFalling
- Emitted when a fall begins.StopFalling
- Emitted when a fall ends.StartMoving
- Emitted when movement in the play area begins.StopMoving
- Emitted when movement in the play area endsStartColliding
- Emitted when the body collider starts colliding with another game objectStopColliding
- Emitted when the body collider stops colliding with another game object
Unity Events
Adding the VRTK_BodyPhysics_UnityEvents
component to VRTK_BodyPhysics
object allows access to UnityEvents
that will react identically to the Class Events.
OnStartFalling
- Emits the StartFalling class event.OnStopFalling
- Emits the StopFalling class event.OnStartMoving
- Emits the StartMoving class event.OnStopMoving
- Emits the StopMoving class event.OnStartColliding
- Emits the StartColliding class event.OnStopColliding
- Emits the StopColliding class event.
Event Payload
GameObject target
- The target the event is dealing with.
Class Methods
ArePhysicsEnabled/0
public bool ArePhysicsEnabled()
- Parameters
- none
- Returns
bool
- Returns true if the body physics will interact with other scene physics objects and false if the body physics will ignore other scene physics objects.
The ArePhysicsEnabled method determines whether the body physics are set to interact with other scene physics objects.
ApplyBodyVelocity/3
public void ApplyBodyVelocity(Vector3 velocity, bool forcePhysicsOn = false, bool applyMomentum = false)
- Parameters
Vector3 velocity
- The velocity to apply.bool forcePhysicsOn
- If true will toggle the body collision physics back on if enable body collisions is true.bool applyMomentum
- If true then the existing momentum of the play area will be applied as a force to the resulting velocity.- Returns
- none
The ApplyBodyVelocity method applies a given velocity to the rigidbody attached to the body physics.
ToggleOnGround/1
public void ToggleOnGround(bool state)
- Parameters
bool state
- If true then body physics are set to being on the ground.- Returns
- none
The ToggleOnGround method sets whether the body is considered on the ground or not.
TogglePreventSnapToFloor/1
public void TogglePreventSnapToFloor(bool state)
- Parameters
bool state
- If true the the snap to floor mechanic will not execute.- Returns
- none
The PreventSnapToFloor method sets whether the snap to floor mechanic should be used.
IsFalling/0
public bool IsFalling()
- Parameters
- none
- Returns
bool
- Returns true if the body is currently falling via gravity or via teleport.
The IsFalling method returns the falling state of the body.
IsMoving/0
public bool IsMoving()
- Parameters
- none
- Returns
bool
- Returns true if the user is currently walking around their play area space.
The IsMoving method returns the moving within play area state of the body.
IsLeaning/0
public bool IsLeaning()
- Parameters
- none
- Returns
bool
- Returns true if the user is considered to be leaning over an object.
The IsLeaning method returns the leaning state of the user.
OnGround/0
public bool OnGround()
- Parameters
- none
- Returns
bool
- Returns true if the play area is on the ground and false if the play area is in the air.
The OnGround method returns whether the user is currently standing on the ground or not.
Example
VRTK/Examples/017_CameraRig_TouchpadWalking
has a collection of walls and slopes that can be traversed by the user with the touchpad but the user cannot pass through the objects as they are collidable and the rigidbody physics won't allow the intersection to occur.
Updated less than a minute ago