2DWorlds Forums
Homing Object: Making One Part Go to Another - Printable Version

+- 2DWorlds Forums (http://2dworlds.buildism.net/forum)
+-- Forum: 2DWorlds (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=4)
+--- Forum: Scripting (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=13)
+--- Thread: Homing Object: Making One Part Go to Another (/showthread.php?tid=8524)



Homing Object: Making One Part Go to Another - Qwertygiy - 11-01-2011

For Mustachio:

[lua]
local target = game.World.Box
local bullet = game.World.Circle
local ups = 10 --units per second

--change those three variables as needed

while true do
sleep(0.01)
local xdistance = target.Position.x - bullet.Position.x
if xdistance < 0 then
xdistance = xdistance * -1
end
local ydistance = target.Position.y - bullet.Position.y
if ydistance < 0 then
ydistance = ydistance * -1
end
if xdistance < 0.1 and ydistance < 0.1 then break end


if target.Position.x > bullet.Position.x then

if target.Position.y > bullet.Position.y then
bullet.Position = bullet.Position + Vec2D(ups/100, ups/100)
else
bullet.Position = bullet.Position + Vec2D(ups/100, ups/-100)
end

else

if target.Position.y > bullet.Position.y then
bullet.Position = bullet.Position + Vec2D(ups/-100, ups/100)
else
bullet.Position = bullet.Position + Vec2D(ups/-100, ups/-100)
end

end

end

print("Parts reached")
[/lua]