Teleport First person controller in the same scene, HELP!

Hello !
i’m a beginner with Unity 3D, i work on a simple game with two island, and i try to teleport my first person controller from a point of my terrain to an another point of the same terrain.
To activate the teleport i have made two teleport object (a plane with collider) and i have create the Javascript :

static var logVar:String;
var target1: Transform; 
var target2: Transform; 
var speed = 50.0;
var jumpSpeed = 8.0;
var gravity = 10;
var nextTeleport = Time.time;

function OnControllerColliderHit(hit:ControllerColliderHit){	
   var destpoint : Vector3;
   var movePos : Vector3;
   var startPos : Vector3;
   var therotation : Vector3;
	if(hit.gameObject.tag == "teleport1"  nextTeleport<Time.time)
	{
		movePos = Vector3(target2.position.x+10,100,target2.position.z+10); 
		startPos = Vector3(transform.position.x, 0, transform.position.z);
		therotation = Vector3.Slerp(startPos, movePos, 1);
		transform.LookAt(therotation);
		nextTeleport=Time.time+30.0;
            
		StopCoroutine("MovePlayer2");
		StartCoroutine("MovePlayer2", movePos); 
		logVar="teleport2";
	}
	if(hit.gameObject.tag == "teleport2"  nextTeleport<Time.time)
	{
		movePos = Vector3(target1.position.x+10,100,target1.position.z+10); 
		startPos = Vector3(transform.position.x, 0, transform.position.z);
		therotation = Vector3.Slerp(startPos, movePos, 1);
		transform.LookAt(therotation);
		nextTeleport=Time.time+30.0;
            
		StopCoroutine("MovePlayer2");
		StartCoroutine("MovePlayer2", movePos); 
		logVar="teleport1";
	}
}
function MovePlayer2(movePos : Vector3) {
   var controller : CharacterController = GetComponent(CharacterController);
   var dist = Vector3.Distance (transform.position, movePos);
   var direction = transform.TransformDirection(Vector3.forward);
   for (i = 0.0; i < 1.0; i += (speed * Time.deltaTime) / dist)
   {

   controller.Move(direction * speed);
   yield;
   }
} 

@script RequireComponent(CharacterController)

my 2 teleport object have a tag “teleport1” and “teleport2”

Can some one help to move my first person controller from a point to another point, please ?

why not:

transform.position = target1.position;
//to avoid stuck in the middle of teleport you can rise position like: transform.position =(target1.position.x,target1.position.y + 20,target1.position.z)

try it :wink:

Thanks Jaxas, but it does’nt work.
I try this with your example code :

static var logVar:String;
var target1: Transform; 
var target2: Transform; 

function OnControllerColliderHit(hit:ControllerColliderHit){	
	if(hit.gameObject.tag == "teleport1")
	{
		logVar="teleport2";
		transform.position = target2.position;
	}
	if(hit.gameObject.tag == "teleport2")
	{
		logVar="teleport1";
		transform.position = target1.position;
	}
}

The collision is detected because i display the content of logVar, but my first person controller does’nt move.
Have you an idea ?

The first thing that comes to mind is that you go to the other teleport, hit it the next frame, return to the first teleport, etc…

Try sending yourself to Vector3.zero to test this.

I try to send to Vector3.zero with like GargerathSunman said :

transform.position = Vector3.zero;

but nothing has changed, my first person controller does’nt move anymore.

function OnControllerColliderHit(hit:ControllerColliderHit){	
	if(hit.gameObject.tag == "teleport1")
	{
		logVar="teleport2";
		transform.position = Vector3.zero;
	}
	if(hit.gameObject.tag == "teleport2")
	{
		logVar="teleport1";
		transform.position = Vector3.zero;
	}
}

What’s this script attached to? If the message is changed, then something is being sent.

this script is attached to the first person controller object
i try this code

transform.Translate(Vector3.up * Time.deltaTime, Space.World);

but this does’nt work anymore, it’s very strange

Give this package a try, I threw this together as a quick way to show you how teleporters can work. The script that is attached to the cubes is the Teleporters.js script in the scripts folder.

I would have uploaded it here, but I always get the error “tried to upload empty file” so please let me know if this package works. If it does can someone else try to attach it to this thread so I can take it off my file server? thanks

http://www.gamedevonline.com/subsite/Unity/teleportertraining.zip

In short, this is what is in the scene:
4 x flat planes
4 x spheres for home positions
4 x cubes for triggers

Each sphere has a tag, this is used to identify where the player is in the world with regards to which plane they are on and teleporting from, the 4 spheres are the Home spheres which are the entry point when the player is teleported to that plane. and also have tags.

Teleportation sequence happens clockwise, from the plane you start on, you move to the square, it teleports you north, then that one east, then that one south and finally west. I use this method in several of my projects and it works very well.

I am very positive that the more advanced developers in the forums can elaborate and expand or totally redo it down to like 3 lines of code or something like that, but this is down and dirty way to show you one way to skin this cat.

If you just want the code that is used in the package: (although probably meaningless without looking at the package setup)

// We have 4 different home positions, one for each plane
// This is the BALLS on the board, they don't need to be rendered
var Home1 : GameObject;
var Home2 : GameObject;
var Home3 : GameObject;
var Home4 : GameObject;

// Have to get ahold of our player
var PlayerUnit : GameObject;

// Where we want him to land
var NewPosition: Transform;

// Once the player enters the triggered spot (SQUARE), teleport him
function OnTriggerEnter (other : Collider) {
	PlayerUnit = other.gameObject;

	// We use TAGS on our game object, so we know which one we are dealing with
	// this same code is attached to all SQUARES on the board
	switch(this.gameObject.name)
	{
		case "Teleport1":
			NewPosition = Home2.transform;
				break;
		case "Teleport2":
			NewPosition = Home3.transform;
			break;
		case "Teleport3":
			NewPosition = Home4.transform;
			break;
		case "Teleport4":
			NewPosition = Home1.transform;
			break;
	}
	// Now we know where we want to send our player
	// send him on his merry way
	PlayerUnit.transform.position.x=NewPosition.position.x;
	PlayerUnit.transform.position.y=NewPosition.position.y;
	PlayerUnit.transform.position.z=NewPosition.position.z;
}

// our startup has to identify the home base locations
function Start()
{
	Home1 = GameObject.FindWithTag("Home1");
	Home2 = GameObject.FindWithTag("Home2");
	Home3 = GameObject.FindWithTag("Home3");
	Home4 = GameObject.FindWithTag("Home4");
}

// any special instructions for our squares goes here
function Update () {
}

thank you very much, your explication was very clear and all works fine now.
thanks a lot to every body