0 votes
    if global_position.y >= pos_ini_y + 8 and (!Input.is_action_pressed(action) or !held):
        held = false
        velocity.y = -500
    else:
        velocity.y = 0

It was soposed to stop when the global position of the player is equals to the pos-ini-y(wich is 168). However, it goes a little bit further, if I speed up it goes even further.

At the speed of -500 it next to 160.003
How can I make it get to 168 exactly?

in Engine by (15 points)

As i dont know the context, i'm not sure, but can you place a invisible static body there to stop the movement? Then you don need those complicated cases. Also, you may update position directly after you surpased that position.

Note: isnt this a duplication of this?

Yes, it was duplicated, just hided that one, thanks =)

I don't think in my case I can do the way you suggested.
Last night I tried something like teleporting the player a little each frame, to fit perfeclty I got the remaining part of 1(for 0.25 it would be 0.75) and added this in the end.
I still feel there is a better way to do that...

And can in your game use an invisible static body to stop movement?

I don't think so becouse the field must be opened to simulate a colision with another character.

The problem is that movement in games is not continuous, but kind of calculate the position each frame. So the body may never really be on 168. Could you update the position manually after surpased 168? Something like:

if global_position.y >= pos_ini_y + 8 and (!Input.is_action_pressed(action) or !held):
    held = false
    velocity.y = -500
else:
    velocity.y = 0
    global_position.y = 168

Yoy may also use a invisible static body, and disable the collision after it collided with the object so it does not collide with others. Or use different layer/masks so it does only affects the body you want. IDK how your game is thought.

I made a function based on what you said to make it smoother
Put that on answer, I'm gona give you the best one.
Thanks! =)

i'm glad to help!

1 Answer

+1 vote
Best answer

The problem is that movement in games is not continuous, but kind of calculate the position each frame. So the body may never really be on 168. Could you update the position manually after surpased 168? Something like:

if global_position.y >= pos_ini_y + 8 and (!Input.is_action_pressed(action) or !held):
    held = false
    velocity.y = -500
else:
    velocity.y = 0
    global_position.y = 168

Yoy may also use a invisible static body, and disable the collision after it collided with the object so it does not collide with others. Or use different layer/masks so it does only affects the body you want. IDK how your game is thought.

by (3,443 points)
selected by
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.