Help with your scripting

turn the camera using the mouse.

turn the camera using the mouse.

Postby ant0N » 16 Jul 2014, 12:45

I am trying to create own first person Character Controller using lua. And don't know how to make a review of your mouse.
I have not found a way to rotate the camera (similar entity.turn()). Maybe You have another solution?
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller

Re: turn the camera using the mouse.

Postby StarFire » 16 Jul 2014, 12:58

In most cases we just set the camera to match the object with the dynamic character controller.

Code: Select all
function onUpdate( td )
....
   x,y,z = entity.getPosition(obj);
   qw,qx,qy,qz = entity.getWorldOrientation(obj);
   camera.setPosition(x,y,z);
   camera.setOrientation(qw,qx,qy,qz);
....
end


Try adding the following code to an objects micro script, this script will make a basic FPS with a spawned physics kinematic controller which also will respond to keys[ijkl] or game pad/joystick:

Code: Select all

---------------------------------------------------------------------------------
-- character speeds
walkSpeed          = 3;
runSpeed          = 1.1;
jumpForce         = 10;   -- Initial jump power
turnlimiter         = 0.5;   -- max turning speed
movelimiter         = 1.5;    -- max move speed
flipZ               = -1;   -- direction to face for forward / backward movement

---------------------------------------------------------------------------------
-- character physics
Character_Height       = 1.4;   
Capsule_Offset       = -0.2;   
Character_Width       = 0.3;   
Character_StepSize    = 0.4;   
Character_FallSpeed    = -4;   
Character_SlopeLimit = 60;   
Character_FlipZ       = 0;   
Character_SkinWidth    = 0.1;   
Character_jumpDownForce= 15;   

---------------------------------------------------------------------------------
--Required settings
obj                = 0;
turnSpd             = 0;
shiftDown             = 0;
attacking             = 0;
dccID               = 0;
PU_ID               = 0;
x,y,z                = 0;
move                = 0;
turnVal            = 0;
moveVal             = 0;
running            = false;

---------------------------------------------------------------------------------

function onInit(objID)
   obj = objID;
   controller.setData_Offset(0,Capsule_Offset,0);
   controller.setData_characterHeight(Character_Height);
   controller.setData_shapeType(0);
   controller.setData_characterWidth(Character_Width);
   controller.setData_stepOffset(Character_StepSize);
   controller.setData_gravityY(Character_FallSpeed);
   controller.setData_slopeLimit(Character_SlopeLimit);
   controller.setData_flipDirection(Character_FlipZ);
   controller.setData_skinWidth(Character_SkinWidth);
   controller.setData_upDirectionIndex(1);   
   dccID = controller.spawn(obj);
   controller.setJumpDownforce(dccID,Character_jumpDownForce);
end


function onUpdate( td )
   moveSpd = 0;
   if(attacking == 1) then   controller.move(dccID, 0 ); return;   end
   if(move==1) then moveSpd = walkSpeed*moveVal; end
   x,y,z = entity.getPosition(obj);
   qw,qx,qy,qz = entity.getWorldOrientation(obj);
   camera.setPosition(x,y,z);
   camera.setOrientation(qw,qx,qy,qz);
   entity.turn(obj, 0, turnVal, 0);
   controller.move(dccID, flipZ*moveSpd );
end


---------------------------------------------------------------------------------
-- Helper func's
---------------------------------------------------------------------------------
function valueLimiter(value, limitValue)
   if      (value>limitValue) then value =  limitValue;
   elseif   (value<-limitValue)then value = -limitValue; end
   return value
end

function doMove(state)
   if(state == true) then
      if(running==false) then CURRENT_ANIM = WALK; else CURRENT_ANIM = RUN; end
      move = 1;
   else
      move = 0;
      CURRENT_ANIM = IDLE_1;
   end
end

function doRun(state)
   if(state == true) then
      walkSpeed = runSpeed
      if(move==1)then CURRENT_ANIM = RUN;end
      running = true;
   else
      walkSpeed = 0.4
      running = false;
   end
end


---------------------------------------------------------------------------------
-- Mouse and keyboard input
---------------------------------------------------------------------------------
function onMouseDown( button, x, y )if(button == 1)then attack(); end end
function onMouseUp( button, x, y ) stopAttack() end

function onKeyDown( key )
   if      (key=="shift")       then doRun(true); end
   if       (key == "space")    then controller.doJump(dccID, jumpForce); end
   if       (key == "i")       then doMove(true); moveVal = movelimiter ;
   elseif    (key == "k")       then doMove(true); moveVal =-movelimiter ; end
   if       (key == "j")       then turnVal = turnlimiter ;
   elseif    (key == "l")       then turnVal =-turnlimiter ; end
end

function onKeyUp( key )
   if      (key=="shift") then doRun(false); end
   if       (key == "i") then   doMove(false);
   elseif    (key == "k") then   doMove(false); end
   if       (key == "j") then   turnVal = 0 ;end
   if       (key == "l") then   turnVal = 0 ;end
end
 
 
---------------------------------------------------------------------------------
-- Joystick input
---------------------------------------------------------------------------------
function onJSAxisMoved(axis, value)
   if(axis==0) then
      moveVal = 0;
      if(value ~= 0) then
         moveVal = -value*0.0002;
         moveVal = valueLimiter(moveVal,movelimiter);
         doMove(true);
      else
         doMove(false);
      end
   end
   
   if(axis==1) then
      turnVal =0;
      if(value ~= 0) then
         turnVal = -value*0.00055;
         turnVal = valueLimiter(turnVal,turnlimiter);
      end
   end
end


function onJSButtonPressed(button)
   --sky.print(button)
   if      (button==0) then attack();
   elseif   (button==5) then doRun(true);
   elseif   (button==1) then controller.doJump(dccID, jumpForce); end;
end

function onJSButtonReleased(button)
   if      (button==0) then stopAttack();
   elseif   (button==5) then doRun(false);
   elseif   (button==1) then controller.doJump(dccID, jumpForce); end;
end



Hope this will help ;)
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: turn the camera using the mouse.

Postby StarFire » 16 Jul 2014, 14:28

Also we have a new Lua command sky.getMouseDelta(); which will be in the next update, this will alternatively achieve what you want.

For now to get the same effect you can use something like the following:

Code: Select all
 
lastmx = 0;
lastmy = 0;
function onMouseMove(mx, my)
   dx = mx - lastmx
   dy = my - lastmy
   if(dx < 0) then
      sky.print("mouse is moving left", 1);
   elseif(dx > 0)then
      sky.print("mouse is moving right", 1);
   end

   if(dy < 0) then
      sky.print("mouse is moving down", 1);
   elseif(dy > 0)then
      sky.print("mouse is moving up", 1);
   end   
   lastmx = mx;
   lastmy = my;
end
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: turn the camera using the mouse.

Postby ant0N » 16 Jul 2014, 15:04

Thank you! I'll wait for the next release. :)
User avatar
ant0N
Skyline Moderator
Skyline Moderator
 
Posts: 415
Joined: 02 Nov 2012, 12:49
Location: Россия, Москва
Skill: Programmer
Skill: 3D Modeller


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 3 guests