How can I make the position fit perfectly with move_and_slide()?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Monesi
	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?

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?

p7f | 2018-12-21 10:58

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

Monesi | 2018-12-21 11:10

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…

Monesi | 2018-12-21 11:15

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

p7f | 2018-12-21 11:15

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

Monesi | 2018-12-21 11:18

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.

p7f | 2018-12-21 11:51

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! =)

Monesi | 2018-12-21 12:22

i’m glad to help!

p7f | 2018-12-21 15:20

:bust_in_silhouette: Reply From: p7f

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.