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
CharacterControllervariable 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 aCharacterControlleris 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
VelocityContainerobjects 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
Updatemethod runs every frame and manages climbing logic. -
Begin Climb: It checks if climbing is possible
CanClimband if so, it callsBeginLocomotionto initiate climbing. -
End Climb: If climbing is ongoing and not possible anymore, it calls
EndLocomotionto 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
activateVelocitieslist 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
leftTeleportRayandrightTeleportRayrepresenting left and right teleport controllers. These are hand controllers that players use in VR. -
Teleport Activation Button:
teleportActivationButtondetermines which button triggers teleportation. It's set in the Unity Inspector and represents a button on the VR controller. -
Activation Threshold:
activationTresholdsets how much the activation button needs to be pressed before teleportation triggers. Smaller values make teleportation easier to trigger. -
Ray Interactors:
leftInteractorRayandrightInteractorRayare 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:
EnableLeftTeleportandEnableRightTeleportare 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
leftTeleportRayorrightTeleportRay. - If the ray from the corresponding hand's
XRRayInteractoris hitting something (isLeftInteractorRayHoveringorisRightInteractorRayHovering), 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
ActionBasedControlleras 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
TwoHandGrabInteractablescript 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
XRSimpleInteractableand 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
snapToSecondHandflag 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
onSelectEnteredandonSelectExitedevents.
-
Rotation Calculation: -The
ProcessInteractablemethod calculates the object's rotation based on the two-hand rotation type. IfsnapToSecondHandis 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
LookRotationmethod 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
IsSelectableBymethod 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.
