0 votes

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!

asked Feb 21 in Engine by Godot_Starter (265 points)

2 Answers

+2 votes
Best answer

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

answered Feb 21 by njamster (8,986 points)
selected Feb 21 by Godot_Starter

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

+1 vote

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))
answered Feb 21 by denxi (403 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.