- Home /
Teleporting Player doesn't work as expected?
Hi guys!
So I have a very strange problem again. I wrote a basic teleportation script which is working fine at the first point. I enter the trigger, and it teleports me to a given gameobject's position. However when I do the same on another trigger because I want a way back to my original position, for some reason it teleports me to the first teleport's target coordination. To better understand: Teleport1 - Teleport1Target Teleport2 - Teleport1Target
Obviously I want the Teleport2 to lead to Teleport2Target, but it doesn't even after I make sure that it has the right gameobject as reference in the inspector.
Here is the code:
#pragma strict
private var Player : GameObject;
var Position : Transform;
var delay : float;
private var IsInRange : boolean;
function Start()
{
Player = GameObject.FindGameObjectWithTag("Player");
}
function OnTriggerStay (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
IsInRange = true;
if(delay <= 0)
{
Player.transform.position = Position.position;
}
}
}
function Update()
{
if(delay > 0 && IsInRange) delay -= 1*Time.deltaTime;
if(delay < 0) delay = 0;
}
function OnTriggerExit()
{
IsInRange = false;
}
$$anonymous$$ight not be the problem, but I think 'Position' is a keyword in the unity sdk. It'd be good practice to name it something that's not, example ' var pos : Transform;
Is that what you attach teleporter objects to in the inspector?
So to get this straight, you have 2 Gameobjects (Teleporters) and you set the destination for the Teleport in the Inspector (Position).. Did you make sure that the second Gameobject/Teleporter has the right teleport coordinates? (and not just the same as the first Teleporter)
I tried rena$$anonymous$$g the variable as you suggested, but it still does the same. And yes, I made sure that they have the right objects assigned to the variable. One of them is name TeleportPoint and the other is TeleportPointBackwards, so there is no way to mistake one for the other.
There's an awesome prefab package that has very solid code and can be easily modified if need be. https://sites.google.com/a/dopplerinteractive.com/contact/home/Teleporter_Example.unitypackage?attredirects=0
I checked your package, but it doesn't really fixes my problem. The script in that package differs from $$anonymous$$e so that it teleports player between "2 pads" and not just simply drop him off at a location.
Answer by gogo199432 · May 14, 2014 at 12:06 PM
Okay, so after sleeping I finally found out what was wrong with my teleportation script. There was nothing wrong with it! The only problem was that the second teleport had it's destination in the volume of the first teleporter, this means that the second teleporter did transfer the player to the right place, but before anyone could notice it, the first teleport transfered him again. So I just made the first teleporter to disable after the first transmission, and it is working now correctly :)
Your answer
Follow this Question
Related Questions
Inspector preview overflow 0 Answers
Game works fine in editor, but build fails with 59 error in the console 2 Answers
UnassignedReferenceException when its already assigned 1 Answer
Unity Weird Inspector Layout Behavior: Can't Scroll To the Bottom of Component Fields 8 Answers
Unity4 Ignoring Inspector/Initialized Public Value in Build 1 Answer