2DWorlds Forums
How do I make one part point towards 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: How do I make one part point towards another? (/showthread.php?tid=1487)



How do I make one part point towards another? - Qwertygiy - 03-30-2011

I need to make a script where one box turns and points (rotates so that the top side is facing) at another box. How would I go about doing that? (Paging Jacob_...)


RE: How do I make one part point towards another? - Jacob__mybb_import1 - 03-30-2011

I made a tool to make a part point towards the mouse. Is it close enough to what you need?

[lua]
part = game.World.MyPart

function normalizeVector(vec)
local m = math.sqrt(vec.x * vec.x + vec.y * vec.y)
return Vec2D(vec.x / m, vec.y / m)
end

function dragged(b, pos)
if b == 1 then
local v = normalizeVector(Vec2D(pos.x - part.Position.x, pos.y - part.Position.y))
local a = math.deg(math.atan2(v.y, v.x))
part.Rotation = a
end
end

link(script.Parent.MouseDragged, dragged)
[/lua]


RE: How do I make one part point towards another? - Qwertygiy - 03-31-2011

AWSOME, thanks! Here's my edited code, in case anyone else wants to use it:
[lua]
part = game.World.Box1

function normalizeVector(vec)
local m = math.sqrt(vec.x * vec.x + vec.y * vec.y)
return Vec2D(vec.x / m, vec.y / m)
end

function dragged(b, pos)
if b ~= nil then
local v = normalizeVector(Vec2D(pos.x - part.Position.x, pos.y - part.Position.y))
local a = math.deg(math.atan2(v.y, v.x))
part.Rotation = a
end end

link(game.World.Box2.PropertyChanged, function(prop, value)
if prop == "Position" then
dragged(game.World.Box2, value)
end end)
[/lua]

Box1 is the part that turns, it points at Box2.


RE: How do I make one part point towards another? - Blizzard - 04-03-2011

Sweet. We need the MouseDown event to return the angle of where you clicked, though. Make it a lot easier.


RE: How do I make one part point towards another? - Jacob__mybb_import1 - 04-03-2011

Use the getAngle() function of the tool: http://buildism.net/forum/Thread-2-4-11?highlight=getangle


RE: How do I make one part point towards another? - Blizzard - 04-04-2011

Cool. This will come in handy.