Search Unity

Gear VR: Picking up/Moving objects

Discussion in 'AR/VR (XR) Discussion' started by nokynokes, Mar 20, 2018.

  1. nokynokes

    nokynokes

    Joined:
    Mar 20, 2018
    Posts:
    1
    Hey everyone, I'm fairly new to unity and working on a gear vr app and I'm trying to learn how to interact with items by picking them up using the touch pad controller. I feel like I'm close and was wondering if I can get some insight into the approach I'm taking.

    Here's an image of my scene:


    What I'd like to be able to do is pick up the sphere in the middle. Using the scripts from https://assetstore.unity.com/packages/essentials/tutorial-projects/vr-samples-51519 I've managed to get a ray to point from the touch controller and recognize the sphere as VR interactive item. I added the following script to the sphere:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using VRStandardAssets.Utils;
    4.  
    5. namespace VRStandardAssets.Examples
    6. {
    7.     // This script is a simple example of how an interactive item can
    8.     // be used to change things on gameobjects by handling events.
    9.     public class ExampleInteractiveItem : MonoBehaviour
    10.     {
    11.         [SerializeField] private Material m_NormalMaterial;              
    12.         [SerializeField] private Material m_OverMaterial;                
    13.         [SerializeField] private Material m_ClickedMaterial;              
    14.         [SerializeField] private Material m_DoubleClickedMaterial;        
    15.         [SerializeField] private VRInteractiveItem m_InteractiveItem;
    16.         [SerializeField] private Renderer m_Renderer;
    17.         [SerializeField] private float offset;
    18.  
    19.         private Vector3 OriginalLocation;
    20.         private Vector3 Offset;
    21.         private bool controllerIsHeld = false;
    22.  
    23.         private void Awake ()
    24.         {
    25.             m_Renderer.material = m_NormalMaterial;
    26.         }
    27.  
    28.  
    29.         private void OnEnable()
    30.         {
    31.             m_InteractiveItem.OnOver += HandleOver;
    32.             m_InteractiveItem.OnOut += HandleOut;
    33.             m_InteractiveItem.OnClick += HandleClick;
    34.             m_InteractiveItem.OnDoubleClick += HandleDoubleClick;
    35.             m_InteractiveItem.OnDown += HandleOnDown;
    36.             m_InteractiveItem.OnUp += HandleOnUp;
    37.             OriginalLocation = m_InteractiveItem.transform.position;
    38.             //UpdateLocation.x = m_InteractiveItem.transform.position.x;
    39.         }
    40.  
    41.  
    42.         private void OnDisable()
    43.         {
    44.             m_InteractiveItem.OnOver -= HandleOver;
    45.             m_InteractiveItem.OnOut -= HandleOut;
    46.             m_InteractiveItem.OnClick -= HandleClick;
    47.             m_InteractiveItem.OnDoubleClick -= HandleDoubleClick;
    48.             m_InteractiveItem.OnDown -= HandleOnDown;
    49.             m_InteractiveItem.OnUp -= HandleOnUp;
    50.         }
    51.  
    52.  
    53.         //Handle the Over event
    54.         private void HandleOver()
    55.         {
    56.             Debug.Log("Show over state");
    57.             m_Renderer.material = m_OverMaterial;
    58.         }
    59.  
    60.  
    61.         //Handle the Out event
    62.         private void HandleOut()
    63.         {
    64.             Debug.Log("Show out state");
    65.             m_Renderer.material = m_NormalMaterial;
    66.         }
    67.  
    68.  
    69.         //Handle the Click event
    70.         private void HandleClick()
    71.         {
    72.             Debug.Log("Show click state");
    73.             m_Renderer.material = m_ClickedMaterial;
    74.  
    75.  
    76.            
    77.  
    78.         }
    79.  
    80.         IEnumerator TranslateObj()
    81.         {
    82.            
    83.             for (;;) {
    84.             //m_InteractiveItem.transform.position = Ray.rayEnd + new Vector3 (offset, offset, offset);
    85.                 m_InteractiveItem.transform.Translate(offset,0,0);
    86.                 yield return null;
    87.             }
    88.         }
    89.  
    90.  
    91.         //Handle the DoubleClick event
    92.         private void HandleDoubleClick()
    93.         {
    94.             Debug.Log("Show double click");
    95.             m_Renderer.material = m_DoubleClickedMaterial;
    96.         }
    97.  
    98.         private void HandleOnDown()
    99.         {
    100.             Debug.Log("button holding down");
    101.             controllerIsHeld = true;
    102.             StartCoroutine ("TranslateObj");
    103.             //m_InteractiveItem.transform.Translate (offset, 0, 0);
    104.  
    105.  
    106.         }
    107.  
    108.         private void HandleOnUp()
    109.         {
    110.             controllerIsHeld = false;
    111.             Debug.Log ("button let go");
    112.             StopCoroutine ("TranslateObj");
    113.             m_InteractiveItem.transform.position = OriginalLocation;
    114.         }
    115.  
    116.  
    117.     }
    118.  
    119. }
    such that if the ray hovers over the sphere and I hold a button down, it moves to the left by an offset and when I let go of the trigger while the ray is still hovering over the sphere, it resets to the original position. What i'd like to see is have the position of the sphere change according to wherever the end of the ray is cast. To do this I was thinking of passing a the ray caster into the script and editing the vr eye ray casting script to hold a public value that would represent the end of the ray. I was wondering if this sound like the right way? I apologize if this is common knowledge, but if anyone could point me in a right direction, I would greatly appreciate it!
     
  2. Arsandis

    Arsandis

    Joined:
    Sep 25, 2018
    Posts:
    20
    Hey,
    I am trying t do the same thing. Were you able to figure it out?