Search Unity

Couple questions about older version of MRTK

Discussion in 'VR' started by nicklowkc1, Feb 27, 2019.

  1. nicklowkc1

    nicklowkc1

    Joined:
    Feb 27, 2019
    Posts:
    28
    Hi, I am currently creating a finding hidden object game in VR 360 photo using unity. After searching for some tutorials about how to create a VR 360 photo viewer in a sphere and how to use MRTK, I am still struggling with how to use the MRTK in a better way.
    The current development environment I have:
    1. A HP WINDOWS MIXED REALITY HEADSET
    2.using Unity Version 2017.4.3f1
    3. MRTK package with version 2017.4.3.0
    4. Windows 10 and Visual Studio 2017

    Couple questions about MRTK:
    1. I am following a tutorial guide written in Japanese and created a controller with laser pointer. Below is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.WSA.Input;
    5.  
    6. public class GunControl : MonoBehaviour {
    7.  
    8.     private Vector3 pos;
    9.     private Quaternion rot;
    10.     private uint inputDeviceId;
    11.     private GameObject gaze;
    12.     public float distance;
    13.     private LineRenderer laser;
    14.     private bool changeColor;
    15.    
    16.    
    17.     // Use this for initialization
    18.     void Start () {
    19.         gaze = transform.Find("Gaze").gameObject;
    20.         laser = gameObject.GetComponent<LineRenderer>();
    21.         InteractionManager.InteractionSourceUpdated += InteractionManager_InteractionSourceUpdated;
    22.         InteractionManager.InteractionSourceDetected +=InteractionManager_InteractionSourceDetected;
    23.         changeColor = false;
    24.     }
    25.     private void InteractionManager_InteractionSourceUpdated(InteractionSourceUpdatedEventArgs obj) {
    26.         if(obj.state.source.id== inputDeviceId ) {
    27.             obj.state.sourcePose.TryGetPosition(out pos);
    28.             obj.state.sourcePose.TryGetRotation(out rot);
    29.             rot *= Quaternion.Euler(30,0,0);
    30.         }
    31.     }
    32.  
    33.     private void InteractionManager_InteractionSourceDetected(InteractionSourceDetectedEventArgs obj) {
    34.         if(obj.state.source.handedness == InteractionSourceHandedness.Right ) {
    35.             inputDeviceId = obj.state.source.id;
    36.         }
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update () {
    41.         transform.SetPositionAndRotation(pos, rot);
    42.         laser.SetPosition(0,pos);
    43.         Vector3 angle = rot * Vector3.forward;
    44.         RaycastHit hit;
    45.         if(Physics.Raycast(pos, angle, out hit, distance)) {  //collision with poiter
    46.             gaze.transform.position = hit.point;
    47.             laser.SetPosition(1,hit.point);
    48.             changeColor = true;
    49.  
    50.             if(Input.GetKeyDown("joystick button 15")) {
    51.                 SessionData.AddScore(10);
    52.             }
    53.            
    54.         } else {
    55.             gaze.transform.position = pos + angle * distance;
    56.             laser.SetPosition(1,pos+angle * distance);
    57.             changeColor = false;
    58.         }
    59.  
    60.         if(changeColor == true ) {
    61.             laser.material.color = Color.green;
    62.         } else {
    63.             laser.material.color = Color.red;
    64.         }
    65.        
    66.         if ( Input.GetKeyDown( "joystick button 15" ) ) {
    67.             Debug.Log("Pressed!!");
    68.             //laser.material.color = Color.green;  //change the laser to green when collides with collider
    69.         }
    70.     }
    71.  
    72.    
    73. }
    The above code works well for the laser, input and tracking my controller position and rotation. However, sometimes it couldn't be tracked when i press the unity play button. Is there any part I can add to my code so I can start tracking my controller when i press the unity play button.

    2. I tried to use the MotionController prefab but I also want laser pointing out from the controller. I couldn't find any prefab or code related to laser pointer.

    3. How do I deal with the UI/Dialog prefab that are already-built inside the package? I know how to code for the physics.raycast function only if I am using the code written in question 1. I have tried several example scenes but the hierarchy and the components attached to the object look little bit messy and confusing to me. It's highly appreciated if there are some tutorials about how to interact with those button by using already-built prefab.(for example, what code should i write when pressing the button. I know it works differently with the unity GUI button)

    Sorry for my long questions and bad english.
    Thank you.