VR camera moved to new location on touch trigger.

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?

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

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…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TeleportToIt : MonoBehaviour
{
    public GameObject XRRigGameObject;
    public GameObject teleportToThisGameObject;

    public void MoveGameObject()
    {
        XRRigGameObject.transform.position = teleportToThisGameObject.transform.position;
    }
}

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 :slight_smile:

1 Like

@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!

@cgbenner no problems at all. I’m no Unity expert but if you get stuck give me a shout.

@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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;

public class PointtoPoint : MonoBehaviour
{

    public GameObject XRRigGameObject;
    public GameObject teleportToThisGameObject;
    //public Material[] materials; // allows input of material colors in a set sized array
    public Renderer rend;  // what are we rendering? the cube

    private int index = 1; //initialize at 1, otherwise you have to press the cube twice to change color
    // Start is called before the first frame update
    void Start()
    {

        rend = GetComponent<Renderer>(); // gives functionality for the renderer
        rend.enabled = true; //makes the rendered 3d object visible if enabled

        //make sure the object has the VRTK script attached...

        if (GetComponent<VRTK_InteractableObject>() == null)

        {

            Debug.LogError(message: "PointtoPoint is required to be attached to an Object that has the VRTK_InteractableObject script attached to it");

            return;

        }
                    
        //subscribe to the event.  NOTE: the "ObectTouched"  this is the procedure to invoke if this object is touched..

        GetComponent<VRTK_InteractableObject>().InteractableObjectTouched += new InteractableObjectEventHandler(ObjectTouched);

    }

    //this object has been touched.. so do what ever is in the code..

    void ObjectTouched(object sender, InteractableObjectEventArgs e)
    {
        XRRigGameObject.transform.position = teleportToThisGameObject.transform.position;
    }
}
1 Like