# Noob question, when body enters collision object

godot 4

### Question

how do i teleport the character into a specific position as well as doing a random generated number between 0 and 1? i’ve managed to connect the body entered node to player but i’m a bit confused how to code it properly and i’ve watched tutorials for this and i can’t understand fully. right now i only have player script which handles the movement and animation of the character.

For the random generated number use randi_range()
randi_range docs

``````var random = randi_range(0, 1)
``````

For the teleport you cant modify the position property, something like:

``````var some_position = Vector3(4, 0, 3)
self.position = some_position
``````

that should work.

1 Like

thanks a bunch for the answer, i hate to admit but i didn’t get the suggestion at all in a sense that i did not know where to put those codes but i’ve managed to solve what i want to happen.

right now whenever the player body enters the collision area i get the 50/50 outcome the only thing that i need right now is to reposition the player to a starting point. i tried using noss’s suggestion but whenever my player collides it disappears, i’ve adjusted the code to use vector2d x and y but the player respawns out of the map. i tried using global position and i don’t know the right and correct coding for that to happen but i managed to be able to print the x and y. so if i can get some help with that, that would mean a lot to me.

So i understand that you have the player and something like an area which woks as a door right? Then you connected the door’s signal “body_entered” to the player, that means that the function “_on_door_body_entered” happens inside the code of the player.

If so, probably this should be enough…

``````func _on_door_body_entered(body):
var random_float = randf()
if random_float < 0.5:
self.position = next_position
else:
self.position = reset_position
``````

next_position and reset_position should vectors of course, 2D or 3D depending of your game

again, if the code you show me happens in the same code file for the player there is no need for:

``````if body.name == "player":
``````

that is redundant, and “body” refers to the collider of the player, not the player object.
Try it out!

2 Likes

I really appreciate your help. Thank you so much.

I’m gonna try and give a quick overview of my project. It’s a 2.5 game kinda since i only have a standard portrait game and the scene is pretty much static and only the player can move around. I had set up one collision object to act as a door, initially i made two collisions but it kinda complicates it so i decided to go for 1 only and have it roll for 50/50 outcome. Right now i only have a script for player and it contains only movement and the body entered.

So the problem i have with the vector2d position when i tried that is when the player enters it doesn’t register the x and y that i have with Vector2D(300, 800), the player goes out of the scene and ignores the collision boundary that i had set up. I suspect that the 300,800 is getting registered on the player scene and not the main scene that i had set up hence why in my code i printed the global position. I tried inputing those coords but still i think it registers in the player scene not the main scene. I just can’t figure out how to code the global position properly.

This was my initial script for the position

var x = 300
var y = 800

func _on_door_body_entered(body):
\$player.position.x = x
\$player.position.y = y

There are things that i want to happen and it all happens when the player enters the collision/door, i think about 4 events right now i have one solve which is the 50/50 outcome, position which i’m currently struggling right now, a transition which i think i can just copy from youtube tutorials, updating a frame of a certain sprite which i’m gonna figure out later. I’m sorry if this looks like a spoon feeding, but that’s how i mostly learn by copying things. So i really appreciate your help man.

No need help here anymore, i figured everything out and now i finished the core mechanics of my game and right now i just need to changes the design. Thank you so much

1 Like