How to lock Player character to a floor based on the parent scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By exuin

So, I want to make a simple non-platformer 2D game. There will be a floor, and the player character walks side to side on the floor. However, I still want the character to be able to jump (it will be completely useless though), so I can’t just not take inputs from the up button. Also, the height of the floor will vary from scene to scene, so I can’t just set it as a variable in the player script. I put the floor height as a variable in the parent node’s script, but that means that I can’t test the player scene itself. What’s the best way to go about doing this? Also, I’d like to be able to get the floor height by just dragging the player instance around the parent scene instead of just setting it as a variable.

:bust_in_silhouette: Reply From: njamster

Even if you don’t plan to implement a platformer, I’d recommend you take a look at Godot’s platformer demo. It should introduce you to everything you need.

Judging from your description, there really is no reason to keep the floor height as a variable. Just give it a CollisionShape2D with the right dimensions. Make your player a KinematicBody2D and use move_and_slide to move it (that includes the jumping) and check for collisions with the floor.

Thanks, that project really helped!

exuin | 2020-02-13 01:23