I’ve been following an online tutorial and I’m pretty sure I copied all the code correctly but the movement won’t work, and I don’t think the gravity or collisions do either.
pls help
tutorial I’ve been following: https://www.youtube.com/watch?v=V3iJtpvAOxU&t=579s
My code copy and pasted:
local DIRECTION_RIGHT = 1
local DIRECTION_LEFT = -1
local BASE_VELOCITY = 500
local GRAVITY = 1000
function init(self)
msg.post("#", "acquire_input_focus")
self.velocity = vmath.vector3(0, 0, 0)
self.ground_contact = false
end
function walk(self)
self.velocity.x = BASE_VELOCITY * self.direction
end
function flip(direction)
sprite.set_hflip("#sprite", direction < 0)
end
function animate(action)
if action.pressed then
sprite.play_flipbook("#sprite", "run")
elseif action.released then
sprite.play_flipbook("#sprite", "idle")
end
end
local function clamp(v, mim, max)
if v < min then
return min
elseif v > max then
return max
else
return v
end
end
function fixed_update(self, dt)
self.velocity.y = self.velocity.y - GRAVITY * dt
self.velocity.y = clamp(self.velocity.y, -2000, 2000)
local position = go.get_position()
position = position + self.velocity * dt
go.set_position(position)
self.velocity.x = 0
end
function on_message(self, message_id, message, sender)
if (message_id == hash("contact_point_response"))
and message.other_group == hash(level) then
self.ground_contact = true
self.velocity.y = 0
end
end
function on_input(self, action_id, action)
animate(action)
self.direction = (action_id == hash("right")) and DIRECTION_RIGHT or DIRECTION_LEFT
walk(self)
flip(self.direction)
end