For any feature requests to either the Skyline Engine or the websites and forums please leave your suggestion here.

Extended "DoJump" Lua for characters

Extended "DoJump" Lua for characters

Postby SpiderMack » 06 Dec 2016, 13:30

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).
User avatar
SpiderMack
Skyline Expert
 
Posts: 441
Joined: 02 Dec 2016, 09:15
Skill: Concept artist
Skill: 3D Modeller
Skill: Level Designer
Skill: Scripter

Re: Extended "DoJump" Lua for characters

Postby SolarPortal » 06 Dec 2016, 16:29

Both the character and controller lua libraries have a "setMoveAdvanced(id, 1 or 0)" function which allows you to override the basic move command and instead use the setMoveDirection( eID, x, y, z ) command to issue your own Vector3 direction to move your character however you want.

Cancel the advanced move out by sending 0 into the setMoveAdvanced() to disable the direction control and allow the move to work again.

http://www.chi-ad.com/Skyline/API/Lua/h ... ef112df4e8

Note: After calling setMoveAdvanced and setMoveDirection(), you may need to call move() to issue the calls to the character controllers. these 3 functions will allow you to move and control your character however you see fit :)

Hope this helps :D

Other useful functions for controlling and editing direction are.
  • entity.getForward(eID)
  • entity.getUp(eID)
  • entity.getRight(eID)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Extended "DoJump" Lua for characters

Postby SpiderMack » 06 Dec 2016, 16:35

Hope a tutorial will get.
User avatar
SpiderMack
Skyline Expert
 
Posts: 441
Joined: 02 Dec 2016, 09:15
Skill: Concept artist
Skill: 3D Modeller
Skill: Level Designer
Skill: Scripter

Re: Extended "DoJump" Lua for characters

Postby SolarPortal » 06 Dec 2016, 23:58

I know SpiderMack is no longer available, but i wanted to finish this topic with a working script on Gen2 that gives a TP character that uses the setMoveAdvanced function for extending jump. It isn't double jump but single and it will show the basics of coding your own character.

Code: Select all
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


Took about 2 hours if that as i had too fix bugs in the middle.
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer


Return to Feature Requests

Who is online

Users browsing this forum: No registered users and 4 guests

cron