I’m having a lot of difficulty trying to get horizontal movement working the way I want it to work. Here’s my code:
if self.velocity.x == math.abs(self.velocity.x) then
local speed1 = self.velocity.x + self.input.x * self.move_speed
local speed2 = self.input.x * self.max_speed
self.velocity.x = math.min(speed1, speed2)
else
local speed1 = self.velocity.x + self.input.x * self.move_speed
local speed2 = self.input.x * self.max_speed
self.velocity.x = math.max(speed1, speed2)
end
if self.input.x == 0 then
self.velocity.x = 0
end
Basically I want to be able to move left and right (controlled by the self.input vector) and then move up to max speed. Velocity should be set to 0 when both left and right keys are pressed, or when neither are pressed.
Right now the problem is that the velocity immediately rises to self.max_speed when moving left, but this code has gone through so many iterations over the past few days that I’m convinced there’s something fundamentally wrong with my approach.
How would you solve this problem?