Teleport KinematicBody2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Godot_Starter

I want to set the position of a KinematicBody2D to a x and y coordinate. For example, how to teleport a KinematicBody2D to (1500,-500)?

Thanks for answers!

:bust_in_silhouette: Reply From: denxi

From inside the node:

position = Vector2(x, y)

and from a different node:

TargetNode.position = Vector2(x,y)

Instead of just straight modifying values like that, you can also use functions to do it, like:

set_position(Vector2(x,y))
:bust_in_silhouette: Reply From: njamster

Set it’s global_position-property. Attach a script to your KinematicBody2D:

extends KinematicBody2D

func teleport(to):
    global_position.x = to

Then call this function like this teleport(Vector2(1500,-500)) from somewhere else in the script. If you want to trigger the teleportation from a script attached to a different node, you will need to first get the correct node with get_node().

Booth works, but this is a little bit better, because I have a function, which can be called from everywhere in the script.

Godot_Starter | 2020-02-21 15:21