Search Unity

How to use the Event System with OVR

Discussion in 'Scripting' started by thaovu, Jun 24, 2019.

  1. thaovu

    thaovu

    Joined:
    Jun 21, 2019
    Posts:
    6
    Hi,
    This is my first project in Unity. I read a lot and watched a lot of videos but still could not fix it.
    I want to write a script where I can create dots on a wall.
    The position should be defined by the collision between an object and the raycast from the oculus touch controller.
    I used this tutorial for the visualization https://developer.oculus.com/blog/easy-controller-selection/

    My main problem is that it does not work when I run it. It does not create the dots.
    The ray from the oculus touch controllers are working.
    I don't know if the problem is the calculation of the collision point of the ray and object or maybe the Event System.

    First I tried it with the Update Function. If a button of the touch controller were pushed it should create a dot at the collision position. But that did not work either. The code for that is below.

    I would appreciate your help!

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4.  
    5.  
    6. // Still to do
    7. /*
    8. 1. For creating Particles: Design Prefab
    9. 2. Find a way to solve the problem with eventdata
    10. 3. Implement a solution for connecting the dots
    11.      */
    12.  
    13. public class drawingdots : OVRPhysicsRaycaster, IPointerClickHandler
    14. {
    15.     [Tooltip("Transform for the dots.")]
    16.     public Transform dot = null;
    17.  
    18.    // int primary; // 0 = right controller ; 1= left controller
    19.  
    20.     private List<Vector3> PointList;
    21.     private Vector3 CollisionPos;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.        // eventData = new OVRPointerEventData(EventSystem.current);
    27.        /*
    28.         if (OVRInput.IsControllerConnected(OVRInput.Controller.RTrackedRemote))
    29.         {
    30.             primary = 0;
    31.             eventData.position = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTrackedRemote);
    32.  
    33.         }
    34.         else if (OVRInput.IsControllerConnected(OVRInput.Controller.LTrackedRemote))
    35.         {
    36.             primary = 1;
    37.             eventData.position = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTrackedRemote);
    38.  
    39.         }*/
    40.     }
    41.  
    42.  
    43.     public Vector3 get_CollisionPos(PointerEventData eventData)
    44.     {
    45.  
    46.         //CollisionPos = Position where the ray hits the object
    47.         //calculate the positions
    48.  
    49.         ControllerSelection.OVRRayPointerEventData rayPointerEventData = eventData as ControllerSelection.OVRRayPointerEventData;
    50.  
    51.         var ray = rayPointerEventData.worldSpaceRay;
    52.         float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane;
    53.         var hits = Physics.RaycastAll(ray, dist, finalEventMask);
    54.  
    55.         if (hits.Length > 1)
    56.             System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance));
    57.  
    58.         if (hits.Length != 0)
    59.         {
    60.             CollisionPos = hits[0].point;
    61.         }
    62.         return CollisionPos;
    63.     }
    64.  
    65.     // create particles and save them in the pointlist
    66.     void CreateParticle(PointerEventData eventData, Transform dot)
    67.     {
    68.         Vector3 position = get_CollisionPos(eventData);
    69.         PointList.Add(position);
    70.  
    71.         //create particle with the position
    72.         Instantiate(dot, position, Quaternion.identity);
    73.     }
    74.  
    75.     public List<Vector3> get_List()
    76.     {
    77.         return PointList;
    78.     }
    79.  
    80.     // this function should connect the dots if the buttons of left and right touch controller are pressed
    81.     void ConnectDots()
    82.     {
    83.         //for loop, connecting all particles to a line with help of line renderer or spline
    84.         var list = get_List();
    85.  
    86.        // lineRenderer = GetComponent<LineRenderer>();
    87.  
    88.     }
    89.  
    90.     // debug log information about the position of collision between object and raycast
    91.     void LoggInfo(PointerEventData eventData)
    92.     {
    93.         Vector3 position = get_CollisionPos(eventData);
    94.         // function where all log files are saved
    95.         string numbers = position.ToString();
    96.         Debug.Log("Position: " + numbers);
    97.     }
    98.  
    99.     // eventsystem : if ray from oculus touch collide the wall and button pressed a dot should be created
    100.     void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
    101.     {
    102.         //throw new System.NotImplementedException();
    103.         CreateParticle(eventData, dot);
    104.         LoggInfo(eventData);
    105.     }
    106.  
    107.  
    108.  
    109.     // Update is called once per frame
    110.     //get location where ray hits objects
    111.     //create particle
    112.  
    113.     /*void Update()
    114.      {
    115.          if (primary == 1)
    116.          {
    117.              if (OVRInput.Get(OVRInput.Button.PrimaryHandTrigger))
    118.              {
    119.                  CreateParticle(eventData, dot);
    120.              }
    121.          }
    122.          else if (primary == 0)
    123.          {
    124.              if (OVRInput.Get(OVRInput.Button.SecondaryHandTrigger))
    125.              {
    126.                  CreateParticle(eventData, dot);
    127.              }
    128.          }
    129.  
    130.          LoggInfo(eventData);
    131.  
    132.          // if both button are pushed (left and  right) for connecting the particles
    133.          if (OVRInput.Get(OVRInput.Button.PrimaryHandTrigger) & OVRInput.Get(OVRInput.Button.SecondaryHandTrigger))
    134.          {
    135.              ConnectDots();
    136.          }
    137.      }*/
    138.  
    139. }
     
    Last edited: Jun 24, 2019