Search Unity

VR camera moved to new location on touch trigger.

Discussion in 'VR' started by cgbenner, Aug 5, 2019.

  1. cgbenner

    cgbenner

    Joined:
    Jun 26, 2019
    Posts:
    13
    Hey,

    I'd like to have my user touch an object in the scene, doesn't matter what it is, and be instantly "teleported" to a different specific location. Like a sign that says "Touch here to move to next station". Something like that. I have a script that gets the object touched, using the Oculus touch controllers, and then performs whatever code you put under it. For example, I use it to touch a screen and change the image that is on it. Where I'm hanging up is in having it relocate the camera rig, and if possible select the facing direction. I can relocate the touched object,... but that's not quite what I want. Any ideas?
     
  2. cgbenner

    cgbenner

    Joined:
    Jun 26, 2019
    Posts:
    13
    Not surprising that there are no replies. 4 days later and I'm right where I started. I know this can be done in games, so there must be a way to do it. I've seen Mario do it back in '95! LOL
     
  3. appymedia

    appymedia

    Joined:
    May 22, 2013
    Posts:
    95
    Hey @cgbenner I'll try and help. Did you think about setting the root of your 'rig''s transform to a new location in the code you are running? Something like...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TeleportToIt : MonoBehaviour
    6. {
    7.     public GameObject XRRigGameObject;
    8.     public GameObject teleportToThisGameObject;
    9.  
    10.     public void MoveGameObject()
    11.     {
    12.         XRRigGameObject.transform.position = teleportToThisGameObject.transform.position;
    13.     }
    14. }
    So you set the XRRigGameObject in the Unity script component inspector to the object thats at the root of your rig and create an empty GameObject at the location you want to teleport to (or use an object already at the location) Does that help?

    ** EDIT I forgot to say make sure to set the teleportToThisGameObject in the inspector to where you want to 'teleport' to as well :)
     
    Last edited: Aug 9, 2019
    cgbenner likes this.
  4. cgbenner

    cgbenner

    Joined:
    Jun 26, 2019
    Posts:
    13
    @appymedia

    Thank you for the reply, sorry I didn't see it until this morning. I knew it would involve this type of action, but my coding skills are non-existent, so I just didn't know how I would code it. I will play around with what you shared here, and report back. Thank you!
     
  5. appymedia

    appymedia

    Joined:
    May 22, 2013
    Posts:
    95
    @cgbenner no problems at all. I'm no Unity expert but if you get stuck give me a shout.
     
  6. cgbenner

    cgbenner

    Joined:
    Jun 26, 2019
    Posts:
    13
    @appymedia

    This worked great on the first try! Thank you for the code snippet. I was able to plug that into some code I already had that captures the "touched" object and then performs whatever task with it that is in the rest of the script.

    For anyone else who wants to use it, here is the final code. Remember, I don't write code so much as slap it together from borrowed snippets, so this may not be very elegant:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using VRTK;
    5.  
    6. public class PointtoPoint : MonoBehaviour
    7. {
    8.  
    9.     public GameObject XRRigGameObject;
    10.     public GameObject teleportToThisGameObject;
    11.     //public Material[] materials; // allows input of material colors in a set sized array
    12.     public Renderer rend;  // what are we rendering? the cube
    13.  
    14.     private int index = 1; //initialize at 1, otherwise you have to press the cube twice to change color
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.  
    19.         rend = GetComponent<Renderer>(); // gives functionality for the renderer
    20.         rend.enabled = true; //makes the rendered 3d object visible if enabled
    21.  
    22.         //make sure the object has the VRTK script attached...
    23.  
    24.         if (GetComponent<VRTK_InteractableObject>() == null)
    25.  
    26.         {
    27.  
    28.             Debug.LogError(message: "PointtoPoint is required to be attached to an Object that has the VRTK_InteractableObject script attached to it");
    29.  
    30.             return;
    31.  
    32.         }
    33.                    
    34.         //subscribe to the event.  NOTE: the "ObectTouched"  this is the procedure to invoke if this object is touched..
    35.  
    36.         GetComponent<VRTK_InteractableObject>().InteractableObjectTouched += new InteractableObjectEventHandler(ObjectTouched);
    37.  
    38.     }
    39.  
    40.     //this object has been touched.. so do what ever is in the code..
    41.  
    42.     void ObjectTouched(object sender, InteractableObjectEventArgs e)
    43.     {
    44.         XRRigGameObject.transform.position = teleportToThisGameObject.transform.position;
    45.     }
    46. }
     
    appymedia likes this.