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