I’m trying to make a 2D platfomer game and i’m running into some problems, mainly with getting the player to move around a game world. I have this code for player movement left right and jumping but its not working how I intended.
function update(self, dt) --Movement --Normalizing the direction so that diagonal isn't faster than horizontal/vertical if vmath.length_sqr(self.input)> 1 then self.input = vmath.normalize(self.input) end -- setting the acceleration to 200 pixels per second in the direction of the player's input local acceleration = self.input * 200 local dv = acceleration * dt local v0 = self.velocity local v1 = self.velocity + dv local movement = (v0 + v1) * dt * 0.5 local p = go.get_position() go.set_position(p + movement) self.velocity = v1 -- So input veolcity next time is the final velocity of the previous time self.input = vmath.vector3() -- Changes the vector back to 0 for next useage end
When I press a key the player moves in a direction forever and never stops and when I repeatedly press a key it gets faster. When i hold down a key its like I’ve only pressed it once. I don’t know what to do. Any help would be appreciated.