The entirety of the script controlling my character (mostly based on the tutorialscript):
If there is indeed an error in a script that is causing this, it should be in there.
The game itself is also rather minimal, as i’ve just started out. It would be hard to post it though.
local move_acceleration = 5
local air_acceleration_factor = 0.8
local max_speed = 15
local gravity = -7
local jump_takeoff_speed = 50
-- pre-hashing ids improves performance
local msg_contact_point_response = hash("contact_point_response")
local msg_animation_done = hash("animation_done")
local group_Ground = hash("Ground")
local group_Walls = hash("Walls")
local group_Breakable = hash("Breakable")
local input_left = hash("Left")
local input_right = hash("Right")
local input_jump = hash("Jump")
local input_rush = hash("Rush")
local input_glide = hash("Glide")
local animation_done_message = hash("animation_done")
function init(self)
msg.post(".", "acquire_input_focus")
self.velocity = vmath.vector3(0, 0, 0)
self.correction = vmath.vector3()
self.ground_contact = false
self.move_input = 0
self.anim = nil
self.hit_roof = false
self.face_left = false
self.rushing = false
self.gliding = false
self.animating = false
print("finished init")
end
function update(self, dt)
if self.face_left then
sprite.set_hflip("#sprite", true)
else
sprite.set_hflip("#sprite",false)
end
msg.post("camera","look_at",{ position = go.get_position()})
local target_speed = self.move_input * max_speed
local speed_diff = target_speed - self.velocity.x
local acceleration = vmath.vector3(0, gravity, 0)
if self.gliding and self.velocity.y <= 0 then
acceleration.y = acceleration.y/9
end
if speed_diff ~= 0 then
if speed_diff < 0 then
acceleration.x = -move_acceleration
else
acceleration.x = move_acceleration
end
if not self.ground_contact then
acceleration.x = air_acceleration_factor * acceleration.x
end
end
local dv = acceleration * dt
if math.abs(dv.x) > math.abs(speed_diff) then
dv.x = speed_diff
end
local v0 = self.velocity
self.velocity = self.velocity + dv
if self.rushing then
if self.face_left then
self.velocity.x = -max_speed-10
else
self.velocity.x = max_speed+10
end
end
local dp = (v0 + self.velocity) * dt * 0.5
go.set_position(go.get_position() + dp)
self.correction = vmath.vector3()
self.move_input = 0
if self.gliding and not self.ground_contact then
msg.post("#sprite","play_animation",{id = hash("Gliding")})
elseif self.rushing then
msg.post("#sprite","play_animation",{id = hash("Rushing")})
else
local moving = not (self.velocity.x == 0) and not (self.gliding or self.rushing)
if moving and not self.animating then
msg.post("#sprite", "play_animation", {id = hash("Running")})
self.animating = true
elseif not moving and not self.animating then
msg.post("#sprite","play_animation", {id = hash("Idle")})
end
end
self.ground_contact = false
end
local function handle_obstacle_contact(self, normal, distance)
local proj = vmath.dot(self.correction, normal)
local comp = (distance - proj) * normal
self.correction = self.correction + comp
go.set_position(go.get_position() + comp)
if normal.y > 0.7 then
self.ground_contact = true
self.gliding = false
end
proj = vmath.dot(self.velocity, normal)
if proj < 0 then
self.velocity = self.velocity - proj * normal
end
end
function on_message(self, message_id, message, sender)
if message_id == msg_contact_point_response then
if message.group == group_Ground or message.group == group_Walls then
handle_obstacle_contact(self, message.normal, message.distance)
elseif message.group == group_Breakable then
if self.rushing then
msg.post(message.other_id,"destroy_self")
else
handle_obstacle_contact(self, message.normal, message.distance)
end
end
end
if message_id == animation_done_message then
self.animating = false
end
end
local function jump(self)
if self.ground_contact then
self.velocity.y = jump_takeoff_speed
end
end
local function abort_jump(self)
if self.velocity.y > 0 then
self.velocity.y = self.velocity.y * 0.5
end
end
function on_input(self, action_id, action)
if action_id == input_left then
self.move_input = -action.value
self.face_left = true
self.rushing = false
elseif action_id == input_right then
self.move_input = action.value
self.face_left = false
self.rushing = false
elseif action_id == input_jump then
if action.pressed then
jump(self)
elseif action.released then
abort_jump(self)
end
elseif action_id == input_rush then
self.rushing = true
elseif action_id == input_glide then
if not self.ground_contact and action.pressed then
self.gliding = not self.gliding
end
end
end