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...

3 Answers

3

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 :smiley:

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.

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);

Yes I have collider 2D, and the log didn't send any message.

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.