Search Unity

No collision possible but SelectionRaycast gives back hit.point

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

  1. thaovu

    thaovu

    Joined:
    Jun 21, 2019
    Posts:
    6
    Hi,
    So I solved how to create an Object via Raycast with Oculus.
    What I wanted to do is create an object at the collision point between oculus touch controller and an object via a Selection Ray.

    But now while playing objects are instantiated even though there cannot be a collision.
    It is mid air. I tried the script again in a empty scene with only a plane.
    But it also happen there.
    Most of the time it creates the objects at the right positions.
    I don't know if SelectionRaycast may calculates the wrong collision position (hit.point)

    This is my code. Do you have a idea what could be wrong?

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class drawingdots : MonoBehaviour
    5. {
    6.     [Tooltip("Transform for the dots.")]
    7.     public Transform dot = null;
    8.     [Tooltip("Tracking space of the OVRCameraRig.\nIf tracking space is not set, the scene will be searched.\nThis search is expensive.")]
    9.     public Transform trackingSpace = null;
    10.  
    11.     public OVRInput.Controller activeController = OVRInput.Controller.None;
    12.     private List<Vector3> PointList;
    13.     RaycastHit hit;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         activeController = ControllerSelection.OVRInputHelpers.GetControllerForButton(OVRInput.Button.PrimaryIndexTrigger, activeController);
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         activeController = ControllerSelection.OVRInputHelpers.GetControllerForButton(OVRInput.Button.Any, activeController);
    24.         Ray ray = ControllerSelection.OVRInputHelpers.GetSelectionRay(activeController, trackingSpace);
    25.  
    26.         if (Physics.Raycast(ray, out hit))
    27.         {
    28.             Vector3 position = hit.point;
    29.  
    30.             Debug.Log("Button pushed: " + OVRInput.Get(OVRInput.Button.Any));
    31.             if (OVRInput.Get(OVRInput.Button.Any))
    32.             {
    33.                 Instantiate(dot, position, Quaternion.identity);
    34.                 Debug.Log("Particle Created");
    35.             }
    36.             Debug.Log("Position: " + position);
    37.         }
    38.         else
    39.         {
    40.             Debug.Log("No Collision");
    41.         }
    42.  
    43.  
    44. }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You can print hit gameobject name to the log to see what exactly ray have hitted.
     
  3. thaovu

    thaovu

    Joined:
    Jun 21, 2019
    Posts:
    6
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    thaovu likes this.
  5. thaovu

    thaovu

    Joined:
    Jun 21, 2019
    Posts:
    6
  6. KTS95SL

    KTS95SL

    Joined:
    Apr 30, 2020
    Posts:
    7
    I got the following error.

    The name 'ControllerSelection' does not exist in the current context.