Guest User

Untitled

a guest
Jan 4th, 2022
15,664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. /// <summary>
  6. /// Manages the spatial anchors of the project
  7. /// </summary>
  8. public class SpatialAnchorsManager : MonoBehaviour
  9. {  
  10.     /// <summary>
  11.     /// Update
  12.     /// </summary>
  13.     private void Update()
  14.     {
  15.         bool trigger1Pressed = OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger);
  16.         bool trigger2Pressed = OVRInput.GetDown(OVRInput.Button.SecondaryIndexTrigger);
  17.  
  18.         //if the user has pressed the index trigger on one of the two controllers, generate an object in that position
  19.         if (trigger1Pressed)
  20.             GenerateObject(true);
  21.  
  22.         if (trigger2Pressed)
  23.             GenerateObject(false);
  24.     }
  25.  
  26.     /// <summary>
  27.     /// Generates an object with the same pose of the controller
  28.     /// </summary>
  29.     /// <param name="isLeft">If the controller to take as reference is the left or right one</param>
  30.     private void GenerateObject(bool isLeft)
  31.     {
  32.         //get the pose of the controller in local tracking coordinates
  33.         OVRPose objectPose = new OVRPose()
  34.         {
  35.             position = OVRInput.GetLocalControllerPosition(isLeft ? OVRInput.Controller.LTouch : OVRInput.Controller.RTouch),
  36.             orientation = OVRInput.GetLocalControllerRotation(isLeft ? OVRInput.Controller.LTouch : OVRInput.Controller.RTouch)
  37.         };
  38.  
  39.         //Convert it to world coordinates
  40.         OVRPose worldObjectPose = OVRExtensions.ToWorldSpacePose(objectPose);
  41.  
  42.         //generate an object depending on the controller onto which the trigger was pressed, and assign it the pose of the controller
  43.         GameObject.Instantiate(Resources.Load<GameObject>(isLeft ? "Object1" : "Object2"),
  44.             worldObjectPose.position,
  45.             worldObjectPose.orientation
  46.             );
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment