Lua DoJump for character is awesome and works as intended.
But i need a custom DoJump , need to be able to call it in the air one more time (Double Jump) or call it with custom events ( collision with special wall to do wall jumps).
obj = 0;
walkSpeed = 15;
runSpeed = 45;
jumpAmt = 0.2;
turnSpeed = 150;
idleanim = "";
walkanim = "";
runanim = "";
jumpanim = "";
fallanim = "";
forwardKeyDown = false;
backKeyDown = false;
leftKeyDown = false;
rightKeyDown = false;
shiftKeyDown = false;
spaceKeyDown = false;
isJumping = false;
lastPos = newType.vec3(0,0,0);
currentDirection = newType.vec3(0,0,0);
current_state = 0;
-- System initialisation - Skyline script entry point
function onInit(objID) obj = objID; end
-- Called after all the scenes onInit() have been called.If you need to ensure your data exits, then do you set up here
function postInit()
idleanim = anim.getFromMap(obj, "Idle1");
walkanim = anim.getFromMap(obj, "Walk");
runanim = anim.getFromMap(obj, "Run");
jumpanim = anim.getFromMap(obj, "Jump");
fallanim = anim.getFromMap(obj, "Animal_Feed1"); -- Animal_Feed1 is now mapped for the fall as it wont be used.
anim.playAnimation(obj, idleanim, 0, 0);
end
-- Updated every frame
function onUpdate(timeDelta)
-- Do the forwards backwards physics.
if(isJumping == false)then
if(forwardKeyDown == true and shiftKeyDown == false)then
walk(1);
elseif(forwardKeyDown == true and shiftKeyDown == true)then
run(1);
elseif(backKeyDown == true and shiftKeyDown == false)then
walk(-1);
elseif(backKeyDown == true and shiftKeyDown == true)then
run(-1);
else
idle();
end
if(spaceKeyDown == true)then
startJump(timeDelta);
end
else
doJump();
end
-- Do the entity turn using the entity.turn function
if(leftKeyDown == true)then turn(-1, timeDelta); end
if(rightKeyDown == true)then turn(1, timeDelta); end
x,y,z = entity.getPosition(obj);
lastPos.x = x;
lastPos.y = y;
lastPos.z = z;
end
function idle()
character.move(obj, 0 );
anim.playAnimation(obj, idleanim,30, 1);
current_state = 0;
end
function walk(direction)
character.move(obj, walkSpeed*direction );
if(direction ==1 )then anim.setSpeed(obj, walkanim, 1); elseif(direction ==-1 )then anim.setSpeed(obj, walkanim, -1); end
anim.playAnimation(obj, walkanim, 30, 1);
current_state = 1;
end
function run(direction)
character.move(obj, runSpeed*direction );
if(direction ==1 )then anim.setSpeed(obj, runanim, 1); elseif(direction ==-1 )then anim.setSpeed(obj, runanim, -1); end
anim.playAnimation(obj, runanim, 30, 1);
current_state = 2;
end
function turn( direction, timeDelta )
if(direction == -1)then
entity.turn(obj, 0, turnSpeed*timeDelta, 0)
elseif(direction == 1)then
entity.turn(obj, 0, -turnSpeed*timeDelta, 0)
end
end
function startJump(timeDelta)
entityPos = newType.vec3(entity.getPosition(obj));
-- Work out the direction to jump
currentDirection.x = entityPos.x - lastPos.x;
currentDirection.y = entityPos.y - lastPos.y;
currentDirection.z = entityPos.z - lastPos.z;
currentDirection = newType.vec3(vector3.normalisedCopy(currentDirection.x, currentDirection.y, currentDirection.z));
currentDirection.x = currentDirection.x;
currentDirection.y = currentDirection.y;
currentDirection.z = currentDirection.z;
dirMulti = timeDelta;
if(current_state == 1)then
dirMulti = dirMulti * walkSpeed
elseif(current_state == 2)then
dirMulti = dirMulti * runSpeed
end
jump_td = timeDelta * 5.0;
character.setMoveAdvanced(obj, 1 );
character.setMoveDirection(obj, currentDirection.x*dirMulti, currentDirection.y*dirMulti+jump_td, currentDirection.z*dirMulti);
character.move(obj, 1 );
anim.playAnimation(obj, jumpanim, 50, 1);
isJumping = true;
--The timer will last for the length of part of the animation.
time.startTimer(1, obj, 700); -- ADJUST TO MATCH YOUR JUMP ANIM LENGTH or shorter if it has a duff animation.
end
function doJump()
character.move(obj, 1 );
anim.playAnimation(obj, jumpanim, 50, 1);
end
function finishJump()
character.setMoveAdvanced(obj, 0 );
isJumping = false;
end
function onTimer()
finishJump();
time.stopTimer(1, obj);
end
function onKeyDown(key)
if(key == "w")then forwardKeyDown = true; end
if(key == "s")then backKeyDown = true; end
if(key == "a")then leftKeyDown = true; end
if(key == "d")then rightKeyDown = true; end
if(key == "space")then spaceKeyDown = true; end
if(key == "shift")then shiftKeyDown = true; end
end
function onKeyUp( key )
if(key == "w")then forwardKeyDown = false; end
if(key == "s")then backKeyDown = false; end
if(key == "a")then leftKeyDown = false; end
if(key == "d")then rightKeyDown = false; end
if(key == "space")then spaceKeyDown = false; end
if(key == "shift")then shiftKeyDown = false; end
end
Users browsing this forum: No registered users and 8 guests