Teleport script help!
I have created two teleporter scripts, neither of them work and I do not know why. I basically want to attach a script to a trigger that says if the player enters this trigger move that player to the position of another GameObject (which is the destination).
1: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Teleporter : MonoBehaviour
{
public Transform player;
public Transform destinationZone;
void Start ()
{
player = GetComponent<Transform> ();
destinationZone = GetComponent<Transform> ();
}
void OnTriggerEnter(Collider player)
{
player.transform.position = destinationZone.transform.position;
player.transform.rotation = destinationZone.transform.rotation;
}
}
2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TeleporterForPlayer : MonoBehaviour
{
public GameObject destinationZone;
public GameObject player;
void Start ()
{
destinationZone = GetComponent <GameObject> ();
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Player"))
{
other.transform.position = destinationZone.transform.position;
}
}
}
I tried your script and it seems like the second one works well if you leave the void Start argument out.
But I ain't no pro ^^
Answer by ronanskie · May 02, 2017 at 05:59 PM
I don't have any experience with c#, but I've made a script once in javascript that does exactly what you want to accomplish here.
var targetgameobject : Transform;
var teleport : boolean;
function Update () {
if(teleport == true){
transform.position = targetgameobject.position;
teleport = false;
}
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "teleporter"){
teleport = true;
}
}
You should put the destination gameobject in the 'targetgameobject' variable. After that everything should be working correctly. I assume the script also works in c# if you change 'function' in 'void' and write the variables and parameters in the c# way.
Your answer
Follow this Question
Related Questions
Can't make player character teleport from one side of screen to another. 1 Answer
how to teleport an object,want to teleport an object but it won't work 1 Answer
need help teleporting an object 0 Answers
Script for teleport on collision? 2 Answers
SteamVR Interaction system. Small problem with Teleport points. 3 Answers