This is the repo for the Unity VR project, which aims to build and share an introduction to Unity virtual reality. The repo contains:
- The XRInteraction Toolkit used for interaction system for creating -a VR experience.
- The XR Plugin Management that provides simple management of XR plug-ins.
- The code for Climbing Locomotion.
- The code for Locomotion Controller.
- The code for Two Hand Grab Interactable.
- Author: Enes Bünyamin Çelik.
The tutorial series introduces VR development in Unity. It covers topics like object grabbing, physics, locomotion systems, collider components, object interaction using rays, handling UI elements, distant object interaction, player movement, teleportation etc. Those who know the fundementals of Unity can work on VR development easily.
Note: This project is still under development.
-
CharacterController Setup: The code starts by defining a
CharacterController
variable namedcharacterController
. This is the component that handles character movement and collision detection. -
Awake Method: The Awake method is overridden and it calls a custom method
FindCharacterController
. This method checks if aCharacterController
is assigned. If not, it tries to find it from thexrOrigin
(which is likely the VR camera's parent object). -
Velocity Management: The script manages a list of
VelocityContainer
objects namedactivateVelocities
. These containers hold velocity information from the VR hand controllers. -
Adding and Removing Providers: The script provides methods (AddProvider and RemoveProvider) to add and remove velocity providers of the hand controllers. It prevents duplicate entries in the list.
-
Update Method: The
Update
method runs every frame and manages climbing logic. -
Begin Climb: It checks if climbing is possible
CanClimb
and if so, it callsBeginLocomotion
to initiate climbing. -
End Climb: If climbing is ongoing and not possible anymore, it calls
EndLocomotion
to stop climbing. -
CanClimb Method: Checks if there are any active velocity providers (hand controllers). If there are, it returns true.
-
ApplyVelocity Method: If climbing is ongoing, this method collects the total velocity from the active providers and applies it to the player's position.
-
CollectControllerVelocity Method: Loops through the
activateVelocities
list and adds up all the individual velocities from the providers.
Overall, this script coordinates climbing movement by collecting velocities from hand controllers, applying them to the player's position, and managing the state of climbing. It's an important part of creating a climbing mechanic in a VR experience.
-
Controller Setup: The script defines two properties
leftTeleportRay
andrightTeleportRay
representing left and right teleport controllers. These are hand controllers that players use in VR. -
Teleport Activation Button:
teleportActivationButton
determines which button triggers teleportation. It's set in the Unity Inspector and represents a button on the VR controller. -
Activation Threshold:
activationTreshold
sets how much the activation button needs to be pressed before teleportation triggers. Smaller values make teleportation easier to trigger. -
Ray Interactors:
leftInteractorRay
andrightInteractorRay
are ray interactors used for hit detection. These are components that help detect where the ìnteractor ray hits in the scene to configure if the ray interacting with a UI element or an interactable object. -
Teleport Enable Flags:
EnableLeftTeleport
andEnableRightTeleport
are properties that allow enabling/disabling teleportation for each hand separately. -
Update Method: This method runs every frame and does the following for each teleport controller:
- It checks if the teleport controller exists
leftTeleportRay
orrightTeleportRay
. - If the ray from the corresponding hand's
XRRayInteractor
is hitting something (isLeftInteractorRayHovering
orisRightInteractorRayHovering
), it won't show the teleport visualization. - If the teleport is enabled for the hand and the activation button is pressed more than the threshold, it shows the teleport visualization.
- CheckIfActivated Method: This method takes an
ActionBasedController
as input (representing a VR controller) and checks if the button designated as the teleport activation button is pressed beyond the set threshold.
In simple terms, this script manages teleportation visualization for both left and right hands based on button presses and hit detection. If the activation button is pressed and the teleport isn't blocked by other interactions, the teleport visualization appears. If you disable teleportation for a hand, the visualization won't show for that hand.
Using the script
- We use
TwoHandGrabInteractable
script to provide two hand interaction to an object. - We added two box collider to the object we want to grab and deleted the meshes in order not to see them.
- We unchecked the Is Trigger event from Unity inspector to ensure the colliders are sticked to the object and not fall.
- Variables Setup:
- It defines a list of second-hand grab points
XRSimpleInteractable
and configuration options for two-hand grabbing. - You can choose between rotation types for two hands from Unity inspector: None (no rotation), First (only the first hand rotates), and Second (only the second hand rotates).
- The
snapToSecondHand
flag determines if the object snaps to the second hand's rotation.
- Event Listeners Setup:
- In the Start method, the script sets up event listeners for each second-hand grab point's
onSelectEntered
andonSelectExited
events.
-
Rotation Calculation: -The
ProcessInteractable
method calculates the object's rotation based on the two-hand rotation type. IfsnapToSecondHand
is true, the object's rotation follows the second hand's rotation; otherwise, it combines the second hand's rotation with an initial offset. -
GetTwoHandRotation Function:
- This function calculates the target rotation based on the chosen rotation type.
- It uses the
LookRotation
method to compute a rotation that aligns with the direction between the two hands.
- Two-Hand Grab Handling:
- When the second hand is grabbed
OnSecondHandGrabbed
, the script stores the initial rotation offset to achieve smooth rotation. - When the second hand is released
OnSecondHandRelease
, the second interactor reference is cleared.
- First Hand Grab Handling:
- When the first hand is grabbed
OnSelectEntered
, the initial rotation of the first hand is stored.
-
First Hand Release Handling: When the first hand is released
OnSelectExited
, the second interactor reference is cleared, and the initial rotation is restored. -
Selectable Check:
- The
IsSelectableBy
method checks if the object can be selected by an interactor. - It ensures that the object is selectable only if it's not already grabbed by another hand.
Author: Enes Bünyamin Çelik
Note: I really appreciate Valem for preparing this amazing Tutorial Series years ago.