How do I make one part point towards another?
#1
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_...)
Reply
#2
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]
Reply
#3
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.
Reply
#4
Sweet. We need the MouseDown event to return the angle of where you clicked, though. Make it a lot easier.
Reply
#5
Use the getAngle() function of the tool: http://buildism.net/forum/Thread-2-4-11?...t=getangle
Reply
#6
Cool. This will come in handy.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)