Hi all,
I am having issues when implementing my player to jump but there are issues, also the player would sometimes stutter when moving on the plane.
Any help would be helpful.
Thanks
Below is the part of the code for the player
local DIRECTION_RIGHT = 1
local DIRECTION_LEFT = -1
local BASE_VELOCITY = 175
local RUN_VELOCITY = 250
local GRAVITY = 1000
local INITIALISE_JUMP = 250
local CONTACT_POINT_RESPONSE = hash("contact_point_response")
local CONTACT_RESPONSE = hash("contact_response")
local GROUND = hash("ground")
local HIGHER_ELEVATION = hash("higher elevation")
function init(self)
msg.post("#", "acquire_input_focus") -- tell this component to acquire input focus
self.actions = {}
self.speed = BASE_VELOCITY
self.velocity = vmath.vector3(0, 0, 0)
self.correction = vmath.vector3(0,0,0)
self.contact_ground = false
self.contact_higherelevation = false
self.move = false
self.contact_ladder = false
self.climbing = false
end
-- to safely end the "game" part of the game
function final(self)
msg.post("#", "release_input_focus")
msg.post("#camera", "release_camera_focus")
end
-- play animation unless the same animation is already playing
local function play_animation(self, animation)
if self.current_animation ~= animation then
self.current_animation = animation
sprite.play_flipbook("#sprite", animation)
end
end
-- clamp a number between a min and max value
local function clamp(v, min, max)
if v < min then return min
elseif v > max then return max
else return v end
end
-- gravity
function fixed_update(self, dt)
--gravity
self.velocity.y = self.velocity.y - GRAVITY * dt -- gravity is acceleration so g * dt is velocity
self.velocity.y = clamp(self.velocity.y, -2000, 2000)
if self.contact_ground or self.contact_higherelevation then
self.velocity.y = 0
end
local position = go.get_position() -- get current game object's position
position = position + self.velocity * dt
go.set_position(position) -- set the new position to the current game object
end
--self explanitory as it is for level collision
function level_collision(self, normal, distance)
distance = distance * vmath.length(normal)
if distance > 0 then
local projection = vmath.project(self.correction, normal*distance)
if projection < 1 then
local compensation = (distance - distance * projection) * normal
go.set_position(go.get_position() + compensation)
self.correction = self.correction + compensation
end
end
if math.abs(normal.x) > 0 then
self.velocity.x = 0
end
if normal.y > 0 then
self.contact_ground = true
self.velocity.y = 0
end
if normal.y < 0 then
self.velocity.y = 0
end
end
-- hit detection of ground
function on_message(self, message_id, message, sender)
if message_id == CONTACT_POINT_RESPONSE then
if message.other_group == GROUND then
level_collision(self, message.normal, message.distance)
end
if message.other_group == HIGHER_ELEVATION and not self.climbing then
level_collision(self, message.normal, message.distance)
end
end
end
-- climb ladders
function climb(self)
self.velocity.y = self.climb
end
-- movement
function h_movement(self)
self.velocity.x = self.speed * self.direction
end
local function jump(self)
if self.contact_ground then
self.contact_ground = false
self.velocity.y = INITIALISE_JUMP
end
end
function update(self, dt)
--variable check/reset
self.contact_ground = false
self.contact_higherelevation = false
self.move = false
self.correction.x = 0
self.correction.y = 0
-- animation handling
-- handing walking/running animation
if self.actions[hash("left")] then
play_animation(self, hash("walk"))
h_movement(self)
sprite.set_hflip("#sprite", true)
elseif self.actions[hash("right")] then
play_animation(self, hash("walk"))
h_movement(self)
sprite.set_hflip("#sprite", false)
else
self.velocity.x = 0
end
-- for jumping/falling animation
-- for use when not climbing
if not self.climbing then
if self.velocity.y > 0 then
play_animation(self, hash("jump"))
elseif self.velocity.y < 0 then
play_animation(self, hash("fall"))
end
else
if self.velocity.y ~= 0 then
--play_animation(self, animation)
elseif self.velocity.y == 0 then
end
end
-- idle
if self.velocity.x == 0 and self.velocity.y == 0 and not self.climbing then
play_animation(self, hash("idle"))
end
-- actions
-- run actions
if self.actions[hash("run")] and not self.climbing then
self.speed = RUN_VELOCITY
else
self.speed = BASE_VELOCITY
end
end
function on_input(self, action_id, action)
if action_id then
if action.pressed then
self.actions[action_id] = true
elseif action.relased then
self.actions[action_id] = nil
end
end
-- inputs for walking left and right
if action.pressed then
if action_id == hash("right") then
self.move = true
self.direction = DIRECTION_RIGHT
elseif action_id == hash("left") then
self.direction = DIRECTION_LEFT
end
self.actions[action_id] = true
elseif action.released then
self.actions[action_id] = nil
end
-- inputs for jumping
if action_id == hash("jump") then
if action.pressed then
jump(self)
end
end
-- gun
if action_id == hash('shoot') then
end
-- inputs for falling on high ground
if action_id == hash("fall") and self.contact_higherelevation then
self.contact_higherelevation = false
end
end