# Godot - Collision for character teleportation

I'm currently working on a little game to discover Godot game engine.

It's a Top-Down game and I want my character to be able to teleport through walls.

I thought to dupplicate my character's collision box and to set its position to the teleportation's position but I didn't find a way to check if there is a collision to this new position.

• I would like to know a few things before trying to help you solve your problem. Is your game a 3D or a 2D game ? How do you choose a position to teleport to (mouse click, or some other input) ? Oct 22 '20 at 13:13
• Oh yes sorry for my lack of precision! It's a 2D Top Down game. The player hits the space key and depending on the direction of the player, I'm adding a constant to it's current position. Oct 22 '20 at 13:18

As I am not allowed to comment, it may be a pain for me to answer correctly with the little knowledge I have.

I have tried a few things, I think you may get away with using a RigidBody2D as a base for the player's collision box' duplicate (let's call it a ghost). This node has a get_colliding_bodies function that returns an array. If the array is empty, then you can teleport to the desired location.

you can check it with something like that :

if get_colliding_bodies().size() == 0:
player.teleport_to(position)


To work, this piece of code would require to be in a script attached to the RigidBody2D, with a reference to your player stored in a variable called player. You would also need a function called teleport_to in the player script, but keep in mind this is just an example.

You may want to use this in a properly set function to check at the right time. For instance when the space key is released.

Here I am assuming that everything is pretty much set up in your scene and that the only issue is that you do not know how to check if there is a collision on the arrival position. I hope that would help you.

• I tested your solution and I got an empty array when I should have a collision. According to the documentation about get_colliding_bodies() "The result of this test is not immediate after moving objects [...]". Maybe my problem is here because before calling this method I'm changing the position of the ghost RigidBody2d. Oct 22 '20 at 16:39
• Be sure to check every frame (in '_process(delta)' for instance) and not just when you hit the spacebar. If you read the sentence you quoted until the end, it reads that the collidings objects is updated once per frame, which is fast/often enough. Oct 22 '20 at 19:54

I have no experience with this, however I believe Area2D is intended for this purpose.

Based on the documentation, I guess you can connect the signal body_exited check get_overlapping_bodies and set a flag "can teleport" to true if it gives you an empty array. Also connect to body_entered and set the flag to false there.