How to teleport a RigidBody2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Yeldham
:warning: Old Version Published before Godot 3 was released.

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

:bust_in_silhouette: Reply From: avencherus

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.

:bust_in_silhouette: Reply From: Paul

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 _integrate_forces 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.

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.

BenVella | 2020-09-21 20:29