Jump not working

There is an issue with my jump for my character, when jump is pressed the y value increases but i dont know how to get the player back to the ground.

local jump_speed = 400
-- Initalise the script
function init(self)
	msg.post(".", "acquire_input_focus") 
	self.vel = vmath.vector3()
	self.anim = nil 	
end

-- movement
function update(self, dt)
	local pos = go.get_position() 
	pos = pos + self.vel * dt 
	go.set_position(pos) 

	self.vel.x = 0 
	self.vel.y = 0

end

--keyboard input
function on_input(self, action_id, action)
	if action_id == hash("left") then
		play_animation(self, (hash "walk_hero"))
		sprite.set_hflip("#sprite", true)
		self.vel.x = -200 
	elseif action_id == hash("right") then
		play_animation(self, (hash "walk_hero"))
		sprite.set_hflip("#sprite", false)
		self.vel.x = 200
	elseif action_id == hash("up") then 
		play_animation(self, (hash "jump_hero"))
		self.vel.y = jump_speed
	end
end

thanks

	if self.vel.y > 0 then
		self.vel.y = jump_speed * 0.8

i tried with some code but its not working

You’re not applying any gravity to the character. There are a number of examples and tutorials showing this:

1 Like

i looked at a tutorial and i tried to apply some code however now my character just floats up really slowly , idk what to do

-- It flips the character sprite
local function play_animation(self, animation)
	if self.current_animation ~= animation then
		self.current_animation = animation
		sprite.play_flipbook("#sprite", animation)
	end
end

local gravity = 500
local jump_speed = 400
-- Initalise the script
function init(self)
	msg.post(".", "acquire_input_focus") 
	self.vel = vmath.vector3()
	self.anim = nil 	
end

-- movement
function update(self, dt)
	self.vel.y = self.vel.y + gravity * dt
	local pos = go.get_position() 
	pos = pos + self.vel * dt 
	go.set_position(pos) 

	
	self.vel.x = 0 
	self.vel.y = 0

end

local function jump(self) 
	self.vel.y = jump_speed
	play_animation(self, (hash "jump_hero"))
end

local function stop(self)
	if self.vel.y > 0 then
		self.vel.y = self.vel.y * 0.5
	end
end

--keyboard input
function on_input(self, action_id, action)
	if action_id == hash("left") then
		play_animation(self, (hash "walk_hero"))
		sprite.set_hflip("#sprite", true)
		self.vel.x = -200 
	elseif action_id == hash("right") then
		play_animation(self, (hash "walk_hero"))
		sprite.set_hflip("#sprite", false)
		self.vel.x = 200
	elseif action_id == hash("up") then 
		if action.pressed then
			jump(self)
		elseif action.released then
			stop(self)
		end
	end
end

Note that the Defold coordinate system has origin in the lower left corner of the screen and that moving up on the y-axis increases the y position. What you are doing now is applying a gravity that moves the character up, not down. Set gravity to -500 and try again.

1 Like

I also notice your value for gravity is way too high. Bear in mind that it is applied every frame so you’ll need something like -25 for the gravity, otherwise the player is going to be slammed into the ground the whole time.

Note that the gravity is multiplied by dt!

1 Like

i changed my gravity but now the character just sinks through the floor and his jump isnt that high

Yes, if you’re using kinetic collision shapes you need to handle collision yourself. E.g. handle the case when the player lands on a platform.

There are many tuturials on how to design jump mechnisms online,
here’s one

2 Likes

Have you compared your project to any of the resources I shared?

2 Likes

how do i do that

It’s shown in the tutorials and examples I shared. Have you looked at any of those yet?

1 Like

i got it working using Resolving kinematic collsions in Defold thanks for your help

2 Likes