smt_turnactive = false
smt_direction = game.CurrentCharacter.Direction
smt_dest = game.CurrentCharacter.Position
smt_destobj = game.CurrentCharacter.DestinationObject
function angleDist(a,b)
return math.min(math.min( math.abs(a + 360 - b), math.abs(a - 360 - b)), math.abs(a - b))
end
function smoothTurningMainLoop()
local newstate = game.CurrentCharacter.AnimState
local newdir = game.CurrentCharacter.Direction
local c = game.CurrentCharacter
if smt_turnactive and newdir == smt_direction then
c.DestinationObject = smt_destobj
c.Destination = smt_dest
smt_turnactive = false
end
if not smt_turnactive and smt_direction~=newdir then
local turn = false
if newstate == eWalkAnim and smt_state ~= eWalkAnim then
smt_dest = c.Destination
c.Destination = c.Position
smt_destobj = c.DestinationObject
turn = true
end
if newstate ~= eWalkAnim and smt_state == eWalkAnim then
turn = true
end
if turn then
smt_turnactive = true
local start = smt_direction
if math.abs(newdir - smt_direction - 360) < math.abs(newdir - smt_direction) and math.abs(newdir - smt_direction - 360) < math.abs(newdir - smt_direction +360) then
start = smt_direction + 360
elseif math.abs(newdir - smt_direction + 360) < math.abs(newdir - smt_direction) then
start = smt_direction - 360
end
game.CurrentCharacter.Direction = start
game.CurrentCharacter:to(angleDist(smt_direction, newdir) * 5, {Direction = newdir})
end
end
smt_direction = newdir
[b]smt_state = newstarte[/b]
end
registerEventHandler("mainLoop", "smoothTurningMainLoop")