Help with your scripting

Move object back and forth

Move object back and forth

Postby CreativeOcclusion » 17 Jan 2016, 11:23

I am trying to move a saw back and forth on the z axis...I am moving it from it's world position...it starts at -22.1 and goes to -18.4 just fine...I can't seem to get it to go back to -22.1...I know I am doing something wrong...There may even be a better way to do this than the way I am trying to do it...I would appreciate any help...Here is the code...
Code: Select all
--My Saw Script------------------------

obj = 0 -- | Define a variable for our object ID
moveSaw = 0;
direction = 1.2;
z=0;

function onInit(objID)
   sky.lprint("\nLUA: Move Saw Demo: ACTIVATED");
   SawArmID = entity.getEntityIDFromTag("SawArm_1");
   SawArmBodyID = physics.getBodyID(SawArmID);
   Saw1_ID = entity.getEntityIDFromTag("SawBlade_1");
   Saw1_BodyID = physics.getBodyID(Saw1_ID);

end



function onUpdate(timeDelta)
   
   if(moveSaw)then
      x,y,z = entity.getPosition( SawArmID );
      z=z+(direction)*timeDelta;
      if (z>-22.1)then moveSaw=false;z=-22.1;end
      if (z<-18.4)then moveSaw=false;z=-18.4;end
      entity.setPosition( SawArmID, x, y, z );
      entity.setPosition( Saw1_ID, x, y, z );
      physics.setPosition(SawArmBodyID, entity.getWorldPosition(SawArmID));
      physics.setPosition(Saw1_BodyID, entity.getWorldPosition(Saw1_ID));
      checkSaw()
   end
   
   
end

function checkSaw()
   x,y,z = entity.getPosition( SawArmID );
   if (z==-22.1) then
      moveSaw = true;
      direction = 1.2;
   end
   
   if (z==-18.4) then
      moveSaw = true;
      direction = -1.2;
   end

end
Thanks, CreativeOcclusion
User avatar
CreativeOcclusion
Skyline Warrior
 
Posts: 366
Joined: 22 Jun 2015, 19:34
Location: Texas

Re: Move object back and forth

Postby epsilonion » 17 Jan 2016, 13:02

Maybe not much help but have you thought about animating the saw? and calling the animation, a bit more info would be great for what you're doing is a saw that moves to cut logs on, is it a saw on the player?
User avatar
epsilonion
Skyline Lead Moderator
Skyline Lead Moderator
 
Posts: 874
Joined: 26 Feb 2015, 11:51
Location: Hull, East Yorkshire, England
Skill: Business Manager
Skill: Great creative

Re: Move object back and forth

Postby SolarPortal » 17 Jan 2016, 14:38

I have taken a look at your code and managed to get it to work.
I believe your less than and greater than in the onUpdate loop were backwards.
Code: Select all
-- | old
      if (z>-22.1)then moveSaw=false;z=-22.1;end
      if (z<-18.4)then moveSaw=false;z=-18.4;end

-- | new
      if (z < -22.1)then moveSaw=false; z=-22.1; end
      if (z > -18.4)then moveSaw=false; z=-18.4; end


then in your checksaw(). the if conditions are best used with <= or >= rather than the == operator as an object in movement can sometimes overlap especially with timeDelta.

Code: Select all
   x,y,z = entity.getPosition( SawArmID );
   if (z<=-22.1) then
      moveSaw = true;
      direction = 1.2;
   end
   
   if (z>=-18.4) then
      moveSaw = true;
      direction = -1.2;
   end



The other thing i was going to do but stopped was adding a distance from the current objects z axis value to help remove the hard coded position data. That way you can move your entities anywhere and they still work.

On a sidenote: Have you tried the move action. It contains an axis move and bounce on certain distance and will repetitively do the same thing. It also works on a specific axis like x,y or z. There is also entity.move(obj, x, y, z) . For example move forwards a meter (entity.move(obj, 0, 0, 1 ) or move back a meter (entity.move(obj, 0, 0, -1 ) on the z axis.)

Here is what my code ended up with:
Code: Select all
--My Saw Script------------------------

obj = 0 -- | Define a variable for our object ID
moveSaw = 0;
direction = 1.2;
z=0;

SawArmID = -1;
Saw1_ID = -1;
SawArmBodyID=  -1;
Saw1_BodyID =  -1;
function onInit(objID)
   sky.lprint("\nLUA: Move Saw Demo: ACTIVATED");
end

function postInit()
   SawArmID = entity.getEntityIDFromTag("SawArm_1");
   SawArmBodyID = physics.getBodyID(SawArmID);
   Saw1_ID = entity.getEntityIDFromTag("SawBlade_1");
   Saw1_BodyID = physics.getBodyID(Saw1_ID);
end

function onUpdate(timeDelta)
  checkSaw();
 
   if(moveSaw)then
      x,y,z = entity.getPosition( SawArmID );
      z=z+(direction)*timeDelta;
      if (z < -22.1)then moveSaw=false; z=-22.1; end
      if (z > -18.4)then moveSaw=false; z=-18.4; end
      entity.setPosition( SawArmID, x, y, z );
      entity.setPosition( Saw1_ID, x, y, z );
      physics.setPosition( SawArmBodyID, entity.getWorldPosition(SawArmID) );
      physics.setPosition( Saw1_BodyID, entity.getWorldPosition(Saw1_ID) );
   end
end

function checkSaw()
   x,y,z = entity.getPosition( SawArmID );
   if (z<=-22.1) then
      moveSaw = true;
      direction = 1.2;
   end
   
   if (z>=-18.4) then
      moveSaw = true;
      direction = -1.2;
   end

end


Hope this helps :D
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: Move object back and forth

Postby CreativeOcclusion » 17 Jan 2016, 18:52

Image

Never Mind!!!!...I got it to work...Finally!!!!...Thanks for everything...I could not have done it without your help!!!
Last edited by CreativeOcclusion on 17 Jan 2016, 20:08, edited 3 times in total.
Thanks, CreativeOcclusion
User avatar
CreativeOcclusion
Skyline Warrior
 
Posts: 366
Joined: 22 Jun 2015, 19:34
Location: Texas

Re: Move object back and forth

Postby CreativeOcclusion » 17 Jan 2016, 20:06

Maybe not much help but have you thought about animating the saw?


I would have thought about that ,but it is to hit the player...It is a giant saw...Thanks for the input...
Thanks, CreativeOcclusion
User avatar
CreativeOcclusion
Skyline Warrior
 
Posts: 366
Joined: 22 Jun 2015, 19:34
Location: Texas

Re: Move object back and forth

Postby SolarPortal » 17 Jan 2016, 20:23

Good that you got it working :)
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: Move object back and forth

Postby CreativeOcclusion » 17 Jan 2016, 21:43

I got it figured out...


Now I'm not sure how to setup a collision to damage player...any ideas or example code would be great...Sorry to ask so many questions...
Thanks, CreativeOcclusion
User avatar
CreativeOcclusion
Skyline Warrior
 
Posts: 366
Joined: 22 Jun 2015, 19:34
Location: Texas


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 8 guests