0 votes

I'm making a 2d platformer and I wanted to add a physics based button, but I got stuck when I couldn't make the moving part go only up and down.

Godot version Godot Engine 3.2.3 stable
in Engine by (12 points)

Can you show us your script?

I don't really have a script yet. I wanted to start with this moving part, because my plan was to check the local coordinates of it and based on that information make it activate other things like doors.

Well but you probably have a moving script already where you faced that problem right?

I don't. I wanted to use the rigidbody2ds physics to move it up and down. I set the gravity_scale to negative and made an invisible stopper above it. I then realised that I need it to not move sideways.

Okay, I made it, but it surely is not perfect. From the players perspective it could be weird that a button bounces them back a bit (or bounces other rigidbody2d) + in an edge case if you collide with it at enough high velocity it will go flying to the side. Also I would really like to not unnecessarily overwork the engine.

extends RigidBody2D
onready var start = position

func _integrate_forces(state):
    if position != start:
        state.set_linear_velocity(Vector2((start.x - position.x) * 400, state.get_linear_velocity().y))

You can fix that issue by creating a different layer for the physics based button that isn't masked by the other rigidbodies(the button cant mask them either)

1 Answer

0 votes

If there aren't any movement already, i'd call (my movement variable here).x = 0 not sure if this helps or not.

by (14 points)

Unfortunately it just slows it down for some reason. I think one frame it moves and the other it stops and that cycle repeats. Thank you for help anyways

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.