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.
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.