Im trying to teleport my player between two spots - How do I do this?
Hi all,
So I'm moderately experienced in 3d design, but I do not no much about code. Recently I came to a stand still in my next game, I'm trying to have two worlds set up next to each other in a 3d environment (in the same scene), and have the player transfer back and forth between them. (See attached photo!) What I'm currently trying to do is write a script, either in C# or JavaScript that will:
On press of the control key...
If the player is on the left area, teleport the player object on the x-axis right by 500
If the player is on the right area, teleport the player object on the x-axis left by 500
However, keep the Y and Z positions the same.
I've been thinking I need to use two box colliders to indicate whether the player is on the right or left plane, but I'm not sure. How do I write a script like this? I know this is an incredibly vague question, but any help is GREATLY appreciated!!!
Answer by Fredex8 · Mar 30, 2016 at 09:09 AM
Here's a pretty basic example for you which you can do just by toggling a boolean. It will work fine for what you've described but if there is more than one teleport location you'll obviously need something else.
using UnityEngine;
using System.Collections;
public class TeleportPlayer : MonoBehaviour {
private float distance = 500;
private bool teleported;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.LeftControl))
{
//toggle bool
teleported = !teleported;
//cache player's current position
Vector3 pos = gameObject.transform.position;
if (teleported == true)
{
//move 500 on the X axis, set Y and Z to current position
gameObject.transform.position = new Vector3(pos.x + distance , pos.y, pos.z);
}
else
{
//move -500
gameObject.transform.position = new Vector3(pos.x - distance , pos.y, pos.z);
}
}
}
}
If you've got lots going on in each of those terrains though then it might be better to use separate scenes
Your answer
![](https://koobas.hobune.stream/wayback/20220612073815im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make player fall through platforms? 0 Answers
Object rotates when moving sideways 0 Answers
Touch buttons sometimes won't work 0 Answers
pushback player on collision 1 Answer
Camera Following [2D] 2 Answers