Godot Version
4.2.2
Question
i’ve used the code from this post to move a car in the facing direction (using transform.y instead of x because of the way the sprite faces), and have coded acceleration. as is, the car nearly comes to a stop, but still moves very slightly.
i’ve taken the variables used and the code that handles acceleration here so you can see my current approach
@export var acceleration = 2
@export var deceleration = 2
@export var speed = 5
direction = Input.get_axis("accelerate", "brake")
if direction:
velocity += transform.y * speed * acceleration * direction
# multiplying by the direction flips the value to not always go backwards
else:
if velocity.x > 0:
velocity.x -= deceleration
elif velocity.x < 0:
velocity.x += deceleration
if velocity.y > 0:
velocity.y -= deceleration
elif velocity.y < 0:
velocity.y += deceleration
I’m well aware that this is not the best approach, but I’m new, and don’t know how else to handle it.
At the end of the if else chain, you should set velocity = Vector2.ZERO.
I think that your deceleration may get stuck slightly negative, causing the slight movement.
You can check this on your node in the remote tree view when running your game. You will need to accelerated at least once to have deceleration to happen.
Wouldn’t you need to check whether velocity is low enough, otherwise it would decelerate for a tick, then snap to 0?I’m going to test this, I’m not at my computer, so I can’t test this yet. I’ll update if/when I have
just checked, and that only works for movement in one direction, I’m also wondering if there is a way to lock movement to the axis you’re facing, as I’m trying to replicate the behaviour of a game i made previously on scratch (here’s the game for reference), so drifting along an axis that you aren’t facing wouldn’t be ideal.
you are right about the problem being decimal velocity values below 1, as verified with a print statement, since I couldn’t find the velocity in the remote tree for some reason, and it’s a good thing to have easily accessible without having to navigate the remote node tree every launch.
I figured out a workaround, just make a variable, and instead of adding a velocity to the current one, set the velocity, then multiply it by the new variable. That way you can keep going in the right direction, and have acceleration and deceleration.