Godot version 4.1
###I’m making a 2D platformer type game using a Rigidbody2d, but I can’t figure out how to warp the player back to where he started after he falls off screen.
###I’m making a 2D platformer type game using a Rigidbody2d, but I can’t figure out how to warp the player back to where he started after he falls off screen.
You can just set the position back to the starting position.
But how do I do that?
Try adding a GDscript to the RigidBody2D and change it to:
extends RigidBody2D
func _do_reset():
position = Vector2(1, 1)
Whenever you want to reset the position, call _do_reset()
.
I tried this, by putting this code in:
func _do_reset():
position = Vector2(498, 252)
func _on_out_of_bounds_body_entered(body):
_do_reset()
but this seems to only works for a split second before my character returns to his position he was in before. Did I do something wrong?
Looks like my code is not applicable.
I believe, the posts here describe your use-case and possible solutions:
well, use Marker2D or Node2D node and set the position in editor, get the Marker2D/Node2D position when it needs to respawn to original spot
I think I understand what you’re saying, but I’m pretty new to Godot and coding in general, so can you please explain how to get the marker to teleport the rigidbody when it goes out of bounds?
now i see the issues why it cant, because it’s a rigidbody, cant just .position like any other nodes
The other thread, that I linked mentiones two options:
_integrate_forces
callback and change it to fit your use-caseeither put a parent node2D of this rigidbody and set the position of this parent Node2D
or
in rigidbody’s script of func _integrate_forces(state):
set the position
The integrate forces one sounds like it might work, but unfortunately I have no idea how or where to use it
func _integrate_forces(state):
Vector2(498, 252)
func _on_out_of_bounds_body_entered(body):
_integrate_forces()
I tried this but it didn’t work. Do you know what I did wrong?
yeah, need separate script, the RigidBody2D’s should have a script that look like this
extends RigidBody2D
var original_position=Vector2.ZERO
var is_set_position:bool=true
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func set_to_respawn():
is_set_position=false
func _integrate_forces(state):
if is_set_position:
return
is_set_position=true
self.position=original_position
so when created the rigidbody node, you set the original_position variable to the spawn point
when you need to move the rigidbody back to spawn, call the set_to_respawn() function of this rigidbody
calling can be done by signal or directly rigidbody_node.set_to_respawn()
I think I’ve almost got it, I just don’t know where or how to put the starting position in the code. (I think it’s original_position=Vector2.ZERO, but when I tried to replace ZERO it didn’t work.)
it should be like
rigidbody_node.original_position=Vector2(1,1)
depend where you would want the rigidbody to be spawned
or simply if you want it to record where it spawn without needing to set it like this
just in the rigidbody script again do:
func _ready():
original_position=self.position
pass # Replace with function body.
So replace var original_position=Vector2.ZERO with this??
that’s just placeholder or default value, if you want it to change during runtime, change it from main controller or something or at func _ready like i shown (edited) above
What code do I put in to make the guy go back to the start when he touches the out-of-bounds area? Also, what is bool?? I am so new to Godot that I have like no clue on what this stuff means.
do you have a main controller of the game? like the one controlling everything that’s the upmost script node? or it’s just the rigidbody is the player and the main script?
if that’s the case then, just set the var original_position=
to Vector2 you wanted to respawn at
call/put the set_to_respawn() function when he touches the out of bound area