I am developing a 2D platformer and I am working on the jumping mechanic.
I have used the v = u + at kinematic equation for the formula for a smooth jump.
When I run the game I am able to jump to the left (in the negative direction) smoothly with a gentle curve.
However, when I jump to the right (in the positive direction) the character teleports to the right rather than moving in a smooth gentle curve like when I jump to the left.
Here is the code snippet that handles the jumping:
self.velocity is applied in the update function and self.ground is set to false so the player cannot jump in the air.
self.direction is set when the player moves left or right to identify which way the player is wanting to jump.
Is there any reason for the changes in the jump between negative and positive x values?
Please use the three backticks ``` to define a code segment. Images aren’t very readable, and also not searchable on the forum:
local example = 0
As for your code, I’d suggest keeping a variable self.direction which can be -1, 0 or 1.
Set it to 0 after each update, and set it to either -1 or 1 after the corresponding keypress.
Then the velocity could be calculated like so:
local position = go.get_position()
local pos = position + self.velocity * speed * dt
go.set_position(pos + self.gravity * dt)
self.velocity = vmath.vector3()
Jump handling:
if action_id == hash('space') and self.ground == true then
if self.direction == 'right' then
--v = u + at
self.velocity = self.velocity + vmath.vector3(5,5,0) * 5
self.ground = false
end
if self.direction == 'left' then
--v = u + at
self.velocity = self.velocity + vmath.vector3(-5,5,0) * 5
self.ground = false
end
if self.direction == '' then
--v = u + at
self.velocity = self.velocity + vmath.vector3(0,5,0) * 5
self.ground = false
end
end
Thank you very much for the feedback, I will implement your suggestions and see if it works.