Skip to content

Immerse yourself in the world of VR development with "VR-Learning-Tutorial". This tutorial series covers object interaction, physics, locomotion, UI integration, and more. Perfect for Unity enthusiasts looking to create captivating VR experiences.

License

Notifications You must be signed in to change notification settings

enesbunyamincelik/VR-Learning-Tutorial-Unity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VR Learning Tutorial: An Introduction To Unity VR

License: MIT Made with Unity C#

This is the repo for the Unity VR project, which aims to build and share an introduction to Unity virtual reality. The repo contains:

Overview

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.

Climbing Locomotion

  1. CharacterController Setup: The code starts by defining a CharacterController variable named characterController. This is the component that handles character movement and collision detection.

  2. Awake Method: The Awake method is overridden and it calls a custom method FindCharacterController. This method checks if a CharacterController is assigned. If not, it tries to find it from the xrOrigin (which is likely the VR camera's parent object).

  3. Velocity Management: The script manages a list of VelocityContainer objects named activateVelocities. These containers hold velocity information from the VR hand controllers.

  4. 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.

  5. Update Method: The Update method runs every frame and manages climbing logic.

  6. Begin Climb: It checks if climbing is possible CanClimb and if so, it calls BeginLocomotion to initiate climbing.

  7. End Climb: If climbing is ongoing and not possible anymore, it calls EndLocomotion to stop climbing.

  8. CanClimb Method: Checks if there are any active velocity providers (hand controllers). If there are, it returns true.

  9. ApplyVelocity Method: If climbing is ongoing, this method collects the total velocity from the active providers and applies it to the player's position.

  10. 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.

Locomotion Controller

  1. Controller Setup: The script defines two properties leftTeleportRay and rightTeleportRay representing left and right teleport controllers. These are hand controllers that players use in VR.

  2. Teleport Activation Button: teleportActivationButton determines which button triggers teleportation. It's set in the Unity Inspector and represents a button on the VR controller.

  3. Activation Threshold: activationTreshold sets how much the activation button needs to be pressed before teleportation triggers. Smaller values make teleportation easier to trigger.

  4. Ray Interactors: leftInteractorRay and rightInteractorRay 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.

  5. Teleport Enable Flags: EnableLeftTeleport and EnableRightTeleport are properties that allow enabling/disabling teleportation for each hand separately.

  6. Update Method: This method runs every frame and does the following for each teleport controller:

  • It checks if the teleport controller exists leftTeleportRay or rightTeleportRay.
  • If the ray from the corresponding hand's XRRayInteractor is hitting something (isLeftInteractorRayHovering or isRightInteractorRayHovering), 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.
  1. 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.

Two Hand Grab Interactable

Using the script
  1. We use TwoHandGrabInteractable script to provide two hand interaction to an object.
  2. We added two box collider to the object we want to grab and deleted the meshes in order not to see them.
  3. We unchecked the Is Trigger event from Unity inspector to ensure the colliders are sticked to the object and not fall.
  1. 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.
  1. Event Listeners Setup:
  • In the Start method, the script sets up event listeners for each second-hand grab point's onSelectEntered and onSelectExited events.
  1. Rotation Calculation: -The ProcessInteractable method calculates the object's rotation based on the two-hand rotation type. If snapToSecondHand is true, the object's rotation follows the second hand's rotation; otherwise, it combines the second hand's rotation with an initial offset.

  2. 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.
  1. 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.
  1. First Hand Grab Handling:
  • When the first hand is grabbed OnSelectEntered, the initial rotation of the first hand is stored.
  1. First Hand Release Handling: When the first hand is released OnSelectExited, the second interactor reference is cleared, and the initial rotation is restored.

  2. 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.

Note: I really appreciate Valem for preparing this amazing Tutorial Series years ago.

About

Immerse yourself in the world of VR development with "VR-Learning-Tutorial". This tutorial series covers object interaction, physics, locomotion, UI integration, and more. Perfect for Unity enthusiasts looking to create captivating VR experiences.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published