Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
peterept's avatar
peterept
Protege
12 years ago

Randomly teleport around Tuscany

I created this Teleport.cs script which will jump your player to random locations around the terrain in Tuscany (or any terrain) by pressing R1 bumper on XBox Controller or press Backslash key.

Pretty simple, but I thought I'd share.

To use: Simply drag it onto the OVRPlayerController!

10 Replies

Replies have been turned off for this discussion
  • I haven't tried to do anything random so far in my project but this seems like a great approach. Thanks for sharing. :)

    What's with the adding mesh coliders part? Is the whole terrain in the tuscany demo not colidered up?
  • Hi,

    Tuscany had Colliders on the terrain, but not on the ocean. I added the collider to that (and the villages) so that when raycasting downwards to get the correct placement height on the ground it makes sure the player isn't placed under the water or inside a fake house.

    Is there a better way to detect a ray going through a game object (like the ocean) without a collider?
  • Hey Peterrept,

    nice script which is similar to one I created.
    I am also trying to not only move the player but also set a predefined orientation so that after teleport, it is looking in a set direction.
    Did you look into that ? I can't manage to set the OVRPlayerController rotation or let's say it does not affect the camera direction...
  • DaveA's avatar
    DaveA
    Honored Guest
    "bdeschryver" wrote:
    Hey Peterrept,

    nice script which is similar to one I created.
    I am also trying to not only move the player but also set a predefined orientation so that after teleport, it is looking in a set direction.
    Did you look into that ? I can't manage to set the OVRPlayerController rotation or let's say it does not affect the camera direction...


    I've done this too. You can set the orientation, use oculus.SetYRotation(target.transform.eulerAngles.y); where oculus is the OVRCameraController component and target is the object you put into the scene to mark the teleportation target.
  • Hey DaveA,

    I have a script attached to the OVRPlayerController and in there I wrote this:

    transform.FindChild ("OVRCameraController").SendMessage ("SetYRotation",(balise.transform.eulerAngles.y));

    where "balise" is the object I would like to move to and set orientaztion from.

    But it does not work.
    Can you check it that matches your description in the post above ?
    What could be wrong ?
  • DaveA's avatar
    DaveA
    Honored Guest
    That's the basic idea, but I don't see the context you call this in. It should happen when you are teleporting to the new transform. Can you post your entire script? Or maybe I'll just post mine when I get the chance.
  • DaveA's avatar
    DaveA
    Honored Guest
    Well looking at my old code, it was just using the OVRCameraController, and the OVRPlayerController will override the rotation. So you have to modify OVRPlayerController to add this:

    	public void SetYRotation (float y)
    {
    YRotation = y;
    }


    Then use this script:

    // Teleport.cs by DaveA

    // Drop this on an OVRPlayerController, or not. If not, drag the OVRPlayerController onto this in the Inspector. Or not.
    // Place 'target' transforms in scene where you want to appear. Put them into 'targets' array.
    // Hitting the 'activate key' will jump you there, with target rotation too.
    using UnityEngine;
    using System.Collections.Generic;

    public class Teleport : MonoBehaviour
    {
    public KeyCode activateKey = KeyCode.T;
    public Transform[] targets;
    public Transform player;
    public int curTarget = 0;
    public OVRPlayerController ovrPlayer;

    void Start ()
    {
    if (ovrPlayer == null)
    ovrPlayer = GetComponent<OVRPlayerController>();
    if (ovrPlayer == null)
    ovrPlayer = Object.FindObjectOfType<OVRPlayerController>();
    }

    void Update ()
    {
    if (Input.GetKeyDown(activateKey))
    {
    player.position = targets[curTarget].position;
    ovrPlayer.SetYRotation (targets[curTarget].eulerAngles.y -90f);
    curTarget = ++curTarget % targets.Length;
    }

    }
    }
  • Oh this looks good. I need to try this out. Love the idea of placing the player in curated positions - yet still allowing them to explore a bit from outside that position, before teleporting them to another location.
  • "DaveA" wrote:
    Well looking at my old code, it was just using the OVRCameraController, and the OVRPlayerController will override the rotation. So you have to modify OVRPlayerController to add this:

    	public void SetYRotation (float y)
    {
    YRotation = y;
    }


    Then use this script:

    // Teleport.cs by DaveA

    // Drop this on an OVRPlayerController, or not. If not, drag the OVRPlayerController onto this in the Inspector. Or not.
    // Place 'target' transforms in scene where you want to appear. Put them into 'targets' array.
    // Hitting the 'activate key' will jump you there, with target rotation too.
    using UnityEngine;
    using System.Collections.Generic;

    public class Teleport : MonoBehaviour
    {
    public KeyCode activateKey = KeyCode.T;
    public Transform[] targets;
    public Transform player;
    public int curTarget = 0;
    public OVRPlayerController ovrPlayer;

    void Start ()
    {
    if (ovrPlayer == null)
    ovrPlayer = GetComponent<OVRPlayerController>();
    if (ovrPlayer == null)
    ovrPlayer = Object.FindObjectOfType<OVRPlayerController>();
    }

    void Update ()
    {
    if (Input.GetKeyDown(activateKey))
    {
    player.position = targets[curTarget].position;
    ovrPlayer.SetYRotation (targets[curTarget].eulerAngles.y -90f);
    curTarget = ++curTarget % targets.Length;
    }

    }
    }


    Hey DaveA, extremely cool script! Only hurdle I had was that after attaching the script to the OVRPlayerController, I had to drag the nested OVRCameraController into the 'Player' slot of the script. Then everything worked perfectly. Although I think I had a small rotation issue. The player controller was zeroed out for rotation, as were the empty game objects being used as teleport locations, but the player would teleport to each position, facing 90 degrees to the right of the original orientation. Easily corrected by rotating the position points 90 degrees.

    This is a great way to allow someone to explore a space, but also have the option of teleporting to curated positions, to avoid movement & motion sickness.

    Thanks
  • hmm. I'm seeing issue with where the players eye height has changed after teleporting, and also strange collision issues