How can i make the player enter a boat

Godot Version



I am new to godot and trying to learn while making stuff in the proccess. I have a player and a boat with their separate moving systems all set up. And i am trying to figure out how to make the player actually board the boat.

So far i have made the player press E while standing close to the boat change animation to “boatidle” an animation where he is sitting and lock the players movement.

My problem is that i want the player to teleport after pressing E to a specific location “inside” the boat. So i basically want to know how can i teleport the player to a specific location after pressing a button. Also the boat could be ported in different places so i need a way for the player to teleport on top of the boat no matter where it is located.

Code inside boat for detecting if player is close enough:

Code inside player for switching to boat_state :
player interact

player board func

Thanks a lot and i hope my explanation was clear enough for you to understand.

1 Like

maybe via add_child? You could define a Node3D in the boot as a placeholder and insert the player there. But I’m not sure if that would be the best solution.

1 Like

Idk how that would work when the player exits the boat tho. Cause he needs to teleport back to a specific place on exit as well. Also my project is 2D but this doesn’t really effect your solution just wanted to add it

with 2D it is probably easier, then ‘remove_child’, and position the player nearby

Where do you want them to be able to exit? Only on land/docks, or just anywhere including the water?

land/docks. I’d like to have a set place for the player to teleport on exit/entering

1 Like

Okay, I think I have a way for you to do it, it’s not complicated, but can you post the code that activates the sitting animation for the player? And are you using a collision to detect if the boat is close enough to the player, how is that being done?

Even with that, if you mean on just how to teleport with a button - in the same method you have for pressing E to activate the sitting animation, you can do:

position = [x,y]

X,Y being the position of where you want them to go. I’m asking about the code and detection setup because that may already be giving you the position you want to teleport the player to!

1 Like

i updated the post with the code if you need anything extra let me know. I have a character state machine for the player and i just made a state for when he is isnide the boat that changes animation and disables movement. and i have a collision to detect if the player is close to the boat. For now the transfer to boat state doesnt check the collision but i can change that easily i just want a way for the player to change position to the boat i can figure out the rest i think.

1 Like

you could do the opposite of your enter code - when the boat’s collider touches an area2d marked as a dock/port (with metadata, such as adding a “dock” tag and searching for it in colliding area2ds, or just by having the relevant area2ds only collide with the boat)

then check if the exit key is pressed, reparent player and move to the docks position (you could use a marker2d/empty node2d or the area2d’s position as the teleport end point)

let me know if you want additional info/clarification, happy to give it :)

1 Like

Ok, so, here is my solution. It may not be a universal, or THE way to do it necessarily, but it should work!

  1. Add a child Node2D on the boat. Position it where you want the player to teleport. This will be the seat position. I am going to post code, and in the example, the seat node is titled “seat”, so when referenced it is $seat.

  2. Add a child Node2D to your dock. The name for it doesn’t matter, so long as it is the very top child of the dock (for my example, though you can change it, so long as you edit the code accordingly). Position the node where you want the player to teleport to when exiting onto the dock.

The code I am posting is to work with this setup in mind.

Boat Code:

Player Code (for boarding method and signal detection):

player boarding code

In the scene the player and the boat share, don’t forget to connect the dock and boat signals to the player.

I forgot to add in your original state machine functions in the board() method, don’t delete those, my code is in addition to that.


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