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!

in Engine by (378 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().

by (10,628 points)
selected by

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:

by (449 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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.