Make the player walk somewhere in a nice looking way.
Posted: Thu Feb 22, 2018 10:47 pm
by eradicator
In my mod i want to make the player walk a short distance towards an object. Currently i'm using on_tick to set the walking_state of the player character each tick. In Singleplayer this works "okaish". The only thing that looks shitty is if the direction is changed too often (oscillating between two of the 8 directions due to angle to target) but i guess i could code around that but... In Multiplayer it looks horribly laggy. My guess is that the lag hiding mechanism reacts too aggressive. It looks like the player is teleported around every half second or so instead of properly walking.
--WALK PLAYER TO WAYPOINT ('E' is my event manager)
E.add_disabled(E.on_tick,'make_player_walk_to_target',function(e)
-- makes players walk towards a waypoint
local nobody_walks = true
-- for pindex,p in pairs(game.connected_players) do
for pindex,p in pairs(game.players) do
local walking = global.players[pindex].walking
local character = p.character
if walking then -- is supposed to walk
if walking.end_tick > e.tick -- has not timed out
and walking.ticks_not_moved < 30 -- is not stuck
and walking.old_distance < 50 -- is in walking distance
and character then -- can walk
if walking.old_distance > 0.5 then -- not yet close enough
nobody_walks = false --keep the walking function enabled
-- local direction = walking.direction
local direction = get_vector_as_direction(walking.target,character.position)
local distance = get_distance(walking.target,character.position)
--detect not moved
if walking.old_distance - distance < 0.1 then -- = not-moved-threshold
walking.ticks_not_moved = walking.ticks_not_moved + 1
else
walking.ticks_not_moved = 0
end
--handle not moved (deviate slightly from bird-line direction)
if walking.ticks_not_moved > 4 then
direction = (direction + ( (e.tick%30 > 13) and -2 or 2) + 8 ) %8 --deviate left/right from current direction
walking.direction = direction
end
-- character.active = false --attempt to fix laggy multiplayer stuttering
character.walking_state = {walking=true, direction = direction}
walking.old_distance = distance
else
-- p.print('Waypoint reached')
gui.open(p)
--TODO: set walking state to nil when finished
global.players[pindex].walking = nil
character.walking_state = {walking=false, direction = direction} --attempt to fix laggy multiplayer stuttering
-- character.active = true
end
else
global.players[pindex].walking = nil --some condition failed, so stop walking
character.walking_state = {walking=false, direction = walking.direction}
-- character.active = true
end
end
end
if nobody_walks then E.disable(E.on_tick,'make_player_walk_to_target') end
end)
So my questions are:
Is there a better way to make the player walk then using walking_state? (biter ai maybe?)
If not is there some hack around the lack hiding? (like temporarily detaching the player character from the player?)
Has anyone done something similar before somewhere?