Hello! This is my first time in the Godot forums, I want to create a platform that falls when a body is on top of it, but when there isn’t a body the platform returns to its original position, how do I do that? Basically, some type of Weight Platform, body on top of it, it falls, no body on top of it, returns to its original position.
Hello, no, well, kind of, a platform that has a specific mass, and when a body, like a player or enemy, lands over it, it starts falling because of the body, but, when that body leaves the platform, it starts going to back to its original position, no, not a spring-constrained platform, well, preferibly, like smooth, when a body is over tip, the platform start falling until the body leaves, then it’s turn back to it’s original possition.
Aaah, I think I know what you want to create now: a platform that simply moves linearly downwards when a body is in contact with it, and otherwise goes up to its original position. Okay.
Should the platform only respond to the body of the player?
Is your player a CharacterBody2D?
Moving the platform is pretty straight forward and there are multiple ways to do it. I recommend making a simple state machine that responds to the platform’s body_entered and body_exited signals. When a body enters, the platform should go down, and when a body exits, the platform should go up. For example:
extends RigidBody2D
@export var fall_speed = 1.0
var original_position: Vector3
var is_falling = false
func _ready():
original_position = global_position
body_entered.connect(move_down)
body_exited.connect(move_to_origin)
func _integrate_forces(state):
if is_falling:
state.linear_velocity = Vector2.DOWN * fall_speed
else:
var to_origin = original_position - state.transform.origin
var max_velocity = to_origin.length() / state.step # Used to prevent the body from overshooting its target
state.linear_velocity = (to_origin.normalized() * speed).limit_length(max_velocity)
func move_down():
is_falling = true
sleeping = false # Ensure the body is not sleeping
func move_to_origin():
is_falling = false
sleeping = false # Ensure the body is not sleeping
I hope that answers your question. Let me know if you need explanations for anything or something doesn’t add up.
Well, the platform can respond to a variety of things, the player, and enemy, etc.
And yes, it’s a CharacterBody2D, and how should be the tree? I’m mean that an RigidBody2D should go first or other node and what are the child nodes. For example:
RigidBody2D
CollisionShape2D
Sprite2D
etc.
Sorry if my English isn’t good, it isn’t my mother language.