Change position of player to create a teleport effect.

So I am trying to make a 2D game where the player has a teleport ability.
I thought that getting the players coordinates and then adding an x value to them is a good way to simulate teleporting.
My problem is that I don’t know how to get the players coordinates and then change them in the direction of moving.

Here is a drawing where I am trying to explain it better.

First, you’ll need to figure out the direction of the player’s movement.

If they have a rigidbody attatched, you can get a Vector3 of their velocity with Rigidbody.velocity.

If they don’t, you could find their velocity with this:

private Vector3 velocity;
private Vector3  previous;

void Update () {
    velocity = ((transform.position - previous).magnitude) / Time.deltaTime;
    previous = transform.position;
}

Source: Statement and Celestium from this thread.

You could also just get their direction of movement from the same place you actually move them. For example, if you’re moving the player via keyboard input, just store the Vector3 from the keyboard before you apply it to the player, like this:

Vector3 movementDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

This is probably the best way to do it, as it won’t be affected by the player moving due to knockback, etc.


Then you could teleport the player like this:

public Vector3 teleportDistance = 5f;

public void Teleport () {
    // We normalize the velocity vector so it has a length of 1,
    // We then multiply it by how far we want to teleport.
    transform.position += velocity.normalized * teleportDistance;
}

I’m going to break the problem down into 2 parts.

Part 1: We need to determine which direct the player is moving

Part 2: We need to teleport the player in that direction

PART 1

The easiest way to determine player direction would be to use Unity’s built in input controls for Horizontal and Vertical movement. Simple recap on how this works: Unity tracks input using either WASD or Arrow Keys; Horizontal input gives you Left/Right or A/D, Vertical Input gives you Up/Down or W/S keys. Unity represents this input from the user with either a value of -1 or 1 respectively. So if we want to read this input from the engine it would look like this:

if (Input.GetAxis("Horizontal") < 0) {
	//Move the player left 
}
if (Input.GetAxis("Horizontal") > 0) {
	//Move player right
}

Now we know what direction the player is moving and we have movement controls set up all in one place.

Part 2

Now that we have the movement set up and we always know which way the player is moving (left or right) we can have them teleport on command. I’ll be using the Space Bar as my trigger for activating a teleport in the following code.

float teleportDistance;

if (Input.GetAxis("Horizontal") < 0 && Input.GetKeyDown(KeyCode.Space)) {
	transform.position = new Vector2(transform.position.x - teleportDistance, transform.position.y);
}

if (Input.GetAxis("Horizontal") > 0 && Input.GetKeyDown(KeyCode.Space)) {
	transform.position = new Vector2(transform.position.x + teleportDistance, transform.position.y);
}

What this does is it determines if we are moving either left or right and if we have pressed the Space Bar. If both of these conditions are true then we set the player’s position to their current position + whatever distance we want to move them.

EDIT:

I should also note that with this code I am assuming that you have a 2D side perspective that allows the player to move left and right similar to Mario games. If you have a top down perspective game let me know and I can help you with the modified code.

Also I am using C# for the example code.