+1 vote

Is there a way to teleport a RigidBody2D from a location to another?
I already tried 'setposition()', but the method that I use to move the body normally(setapplied_force()) seems to undo it.

in Engine by (83 points)

2 Answers

+1 vote
Best answer

set_pos() should work, but you have to put the body to sleep for a frame first. Then after a frame has passed, move it, then take it out of sleep using set_sleeping(true/false)

And worth noting, if you do something like set_linear_velocity() before the move, it will awaken the body, preventing the move.

by (5,266 points)
selected by
+6 votes

Sorry for the necro post but this comes up in the search results a lot and it's a bit misleading.

To make this much easier, you can set the position in the integrateforces function like so:

var new_position = Vector2(0, 0)
func _integrate_forces(state):
    if should_reset:
        state.transform.origin = new_position
        should_reset = false

This way you don't have to worry about sleeping at all.

by (26 points)

This I believe would be the preferred method. I also noted that all that changes is the offset, as one would expect, meaning any forces acting upon said rigidbody prior to teleport, will still be in effect once teleported. setting the linear_velocity to a Vector2() can alter this behavior if unwanted.

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.