- Home /
how do i teleport my player to an empty object?
note i'm new to coding so...i know almost nothing
If you're new to coding you should learn the basics first and look through the unity tutorials.
As shady says: you Need learn to coding first. spend two or three moths learning the basics, and later start a project. You will thanks to us later...
Answer by Arterion · Aug 08, 2017 at 04:57 PM
First of all you should answer the Question : How the Teleport should be triggered ?
If you wan't to teleport the Player in every State of the game by pressing a specific key your code look like this :
private Transform PlayerTransform;
public Transform TeleportGoal;
//-------------------------------
void Start ()
{
PlayerTransform = Gameobject.Find("Player").transform;
// Reference the Player's Transform Component
}
//-------------------------------
void Update ()
{
if(Input.GetKeyDown(KeyCode.E))
{
PlayerTransform.position = TeleportGoal.position
}
}
If you want to teleport the Player, when he moves into a Trigger you can use :
void OnTriggerEnter ()
{
PlayerTransform.position = TeleportGoal.position
}
or :
void OnTriggerStay ()
{
if(Input.GetKeyDown(KeyCode.E))
{
PlayerTransform.position = TeleportGoal.position
}
}
Dont forget to add a collider to the gameobject where you put this script on and check the box "isTrigger";
I hope I could help :D
one more thing when my player touches the trigger it says "The variable TeleportGoal of teleport has not been assigned". so i want to know how to make it go to the object labeled TeleportGoal I haven't had any experience with coding until now and would appreciate a reply.
alright so this error is basically saying that the variable in the inspector has no transform component assigned to it. a transform component is the component with the position and rotation and scale labels on it, so what you need to do is make a new gameobject by right clicking on your hierarchy area and clicking create empty, this should create an empty gameobject, now name it TeleportGoal (name doesn't matter but for convenience...) now drag the TeleportGoal object to the TeleportGoal slot on the gameobject with the script attached to it
Answer by · Aug 08, 2017 at 04:59 PM
What do you mean by "teleport"?
If you want to go to the position of the empty object:
player.transform.position = emptyObject.transform.position;
If you want to make the player a child of the empty object:
player.transform.SetParent(emptyObject.transform);
Answer by JTAGames · Jul 02, 2021 at 03:00 AM
https://www.youtube.com/watch?v=pNGwPrjnwfo
This is a more in depth look at teleportation in Unity. Pretty much the same thing except it is better suited to be used in complete projects.