How do I warp my player character back to where he started in a level?

it looks normal, so there’s no CoffeeCup Scene inside game2d node during runtime?
then something is not right with the code on mainscript.gd

I just dont understand how the coffecup is connected to the code in the main script

mainscript

1 Like

nothing wrong with it, though

because it’s instantiated inside the main scene tree

did you change the main scene to this mainscript gd scene?
because the main scene is not Level anymore

1 Like

but I don’t see it’s name in the script. And I don’t see how the game knows it’s the player… Should I do the thing where I drag and drop it in

tried to drag and drop, didn’t work since it’s not in the main scene :frowning:

not quite got what you wanted, the player node now is in current_player variable, you can do whatever it want or just control it from player script

1 Like

this should work now:

extends RigidBody2D

var original_position=Vector2.ZERO
var is_set_position:bool=true

func _ready():
	await get_tree().create_timer(3).timeout
	set_to_respawn()

func set_to_respawn():
	is_set_position=false

func _integrate_forces(state):
	if is_set_position:
		return
	is_set_position=true
	state.transform=Transform2D(0,original_position)
1 Like

Thanks so much! It works!

Although I don’t fully understand how it works, as far as I can see, the out of bounds area is not in the code, so how does it know when to return to the start?

Also, it only works once per try for some reason. And it seems to just respawn once movement occurs and he’s an amount of time has passed.

var original_position=Vector2(500, 200)
var is_set_position:bool=true
var out_of_bounds

func _respawn_start():
if out_of_bounds==true:
await get_tree().create_timer(0.3).timeout
set_to_respawn()

func set_to_respawn():
if out_of_bounds==true:
is_set_position=false

func _integrate_forces(state):
if is_set_position:
return
is_set_position=true
state.transform=Transform2D(0,original_position)
out_of_bounds=false

func _on_out_of_bounds_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
out_of_bounds = true
Ok, this is what I did, and now I think it works.

Note: I made _respawn_start part of physics process delta. Now I think the only problem is that when he teleports back he’s still rolling along at the same speed as he was going when he dropped, which is to be expected, because he doesn’t technically “spawn in” but just teleports.

when it enters the outbounds area?, then just call the set_to_respawn(), i see you did in the following code using out_of_bounds boolean, can just call set_to_respawn() at _on_out_of_bounds_body_shape_entered function

set the state.linear_velocity=Vector2.ZERO before the state.transform code line. it will reset the speed to zero

1 Like

thanks :+1:

1 Like

i think i know what’s missing
need to add:

func _physics_process(delta):
	if is_set_position:
		return
	self.position=original_position

so other than setting the transform position of the physics server, it still need to set the node’s position

1 Like

What does this do?

to set the rigidbody’s node position to original position, so it matches the physics server transform. also it works for non moving rigidbody with this

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.