Search Unity

How can I use Raycast to create an object in VR?

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

  1. thaovu

    thaovu

    Joined:
    Jun 21, 2019
    Posts:
    6
    Hi there,
    There are some tutorials for this but only without VR.
    I had a lot of problems to get the position from the collider and raycast.
    I copied a part from the OVR Physics Raycast script.
    When I try to run the script, nothing happens.
    Can you help me?

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class drawingdots : OVRPhysicsRaycaster
    6. {
    7.     [Tooltip("Transform for the dots.")]
    8.     public Transform dot = null;
    9.  
    10.    // int primary; // 0 = right controller ; 1= left controller
    11.     int primary;
    12.     private List<Vector3> PointList;
    13.     private Vector3 CollisionPos;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         if(OVRInput.IsControllerConnected(OVRInput.Controller.RTrackedRemote))
    19.         {
    20.             primary = 0;
    21.         }
    22.         else if(OVRInput.IsControllerConnected(OVRInput.Controller.LTrackedRemote))
    23.         {
    24.             primary = 1;
    25.         }
    26.     }
    27.  
    28.  
    29.     public Vector3 get_CollisionPos(PointerEventData eventData)
    30.     {
    31.         //CollisionPos = Position where the ray hits the object
    32.         //calculate the positions
    33.  
    34.         ControllerSelection.RayPointerEventData rayPointerEventData = eventData as ControllerSelection.RayPointerEventData;
    35.  
    36.         var ray = rayPointerEventData.worldSpaceRay;
    37.         float dist = eventCamera.farClipPlane - eventCamera.nearClipPlane;
    38.         var hits = Physics.RaycastAll(ray, dist, finalEventMask);
    39.  
    40.         if (hits.Length > 1)
    41.             System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance));
    42.  
    43.         if (hits.Length != 0)
    44.         {
    45.             CollisionPos = hits[0].point;
    46.         }
    47.         return CollisionPos;
    48.     }
    49.  
    50.     // create particles and save them in the pointlist
    51.     void CreateParticle(PointerEventData eventData, Transform dot)
    52.     {
    53.         Vector3 position = get_CollisionPos(eventData);
    54.         PointList.Add(position);
    55.  
    56.         //create particle with the position
    57.         Instantiate(dot, position, Quaternion.identity);
    58.     }
    59.  
    60.     // debug log information about the position of collision between object and raycast
    61.     void LoggInfo(PointerEventData eventData)
    62.     {
    63.         Vector3 position = get_CollisionPos(eventData);
    64.         // function where all log files are saved
    65.         string numbers = position.ToString();
    66.         Debug.Log("Position: " + numbers);
    67.     }
    68.  
    69.     // Update is called once per frame
    70.     //get location where ray hits objects
    71.     //create particle
    72.  
    73.     void Update()
    74.      {
    75.          if (primary == 1)
    76.          {
    77.              if (OVRInput.Get(OVRInput.Button.PrimaryHandTrigger))
    78.              {
    79.                  CreateParticle(eventData, dot);
    80.              }
    81.          }
    82.          else if (primary == 0)
    83.          {
    84.              if (OVRInput.Get(OVRInput.Button.SecondaryHandTrigger))
    85.              {
    86.                  CreateParticle(eventData, dot);
    87.              }
    88.          }
    89.  
    90.          LoggInfo(eventData);
    91.     }
    92.  
    93. }
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
  3. thaovu

    thaovu

    Joined:
    Jun 21, 2019
    Posts:
    6
    Thank you,
    I watched the videos but I cannot come to a solution.
    I watched similar kind of videos.
    My main problem is the parameter PointerEventData eventData.
    How does the script get it? Do I only have to use an UI event system?
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I recommend you look at the VR Tutorials, and also get the excellent VR Tutorial assets.

    Your problem is that you do not undertand why there is some eventdata, and what they originally needed it for, IIRC, the eventdata contains data from a mouse click that then needs to be converted to a world location.

    In VR, you can proceed much easier. When you know that you want to create an object, use the transform of the pointer (either the handheld device, or the HMD itself) and raycast from that objects position along the forward direction. very easy to implement, and much easier to understand than that pancake crap :)

    Download the free VR stuff from the Asset store, it really, shows you how to do that, and much more. But you have to invest the time to understand it, or it'll be very frustrating (doing VR is one of the high points of Unity, so consider yourself lucky).