How do I make a character in Unity 2D teleport?


I’m making a game in Unity 2D and i’m trying to make a character have a teleport ability. I’m very new to scripting in C# and I can’t find any scripts or tutorials on how to do this.
I would like the player to be able to press space bar and teleport some distance in front of them, or maybe even be able to teleport in any direction depending on what direction they are holding down.
Can someone help me out?

I would also like to be able to connect animations to this, so any help on that would be appreciated

Hi @Ice_Climber_

Your question has multiple parts to its solution.

The Teleporting: “Teleporting” something is basically updating its position to somewhere else. You can achieve this in Unity by updating the objects transform.position by some arbitrary vector. If the vector is big enough (i.e. the human couldn’t possible have moved this far this fast) you have teleportation. Look for a character movement tutorial, like this one

Direction: You can, of course, change the vector depending on the pressed button. Have a look at the Input section in the manual.

Possibility of teleportation: You would also have to check, whether the player can actually teleport the full distance and whether he can actually stand there. E.g you probably don’t want to be able to teleport into a rock, that’s unhealthy. You would have to check your map data for the possibility itself. This is relatively easy but is hugely dependant on your map system.

Animation: You can check out the Animation Overview. Alternatively, Brackeys has a video on that as well

To teleport you just change the character’s position.

 float distance=10;
 transform.position=new Vector2(transform.position.x+distance,transform.position.y); 

You can make distance negative, or apply it to y… you can create methods for making it the direction you want…

       transform.position=new Vector2(transform.position.x,transform.position.y+distance); 
       transform.position=new Vector2(transform.position.x,transform.position.y-distance); 

or many different ways to code it.

Make A C# Script Named as (" Teleport " )



even create physics material 2d

Friction = 10
Bounciness = 0
add these to Teleport Script in sprite component and put physics material 2d in rigidbody 2d/ Polygon Collider 2d, press the sprite to teleport

for animation press (command 6) or (control 6) in mac command 6 in windows control 6 or in top left click window >> animation >> animation then select the gameobject in the hierarchy and create an animation clip and get all images for animation drag them to add propperty and in the dopesheet select how how minutes higher is slower lower number is faster you can even record by record icon and move somthing.