Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you make a part rotate to the mouse's point?

Asked by
wackem 50
8 years ago

I have no idea where to start for this and just need some guidance, how would you make a part rotate to where the mouse is pointing in the workspace?

0
Use part.CFrame = CFrame.new(part.Position,mouse.Hit.p) theCJarmy7 1293 — 8y
1
I mean, this is something that would change the part's rotation according to the mouse if you know how to use it, mouse.Move:connect(function() part.Rotation = mouse.Hit.p end) User#11440 120 — 8y
1
I provided a better answer with a better solution. User#11440 120 — 8y
0
If I helped, it would be awesome if you remembered to hit the accept button. It would really help a lot. User#11440 120 — 8y

1 answer

Log in to vote
2
Answered by 8 years ago

Rotating the Part,


If you want the part to rotate according to the mouse in someway, this worked for me,

-- LocalScript in StarterPack
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local part = game.Workspace:WaitForChild("RotatePart")

mouse.Move:connect(function()
    part.Rotation = mouse.Hit.p-- makes the rotation the mouses position
end)
the above script will NOT make the part move, only rotate.

However,

There is a problem with the above, and that's that when you point your mouse at the sky, or distant objects, it makes the part rotate differently. To fix this, I used the following script,

-- LocalScript in StarterPack
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local part = game.Workspace:WaitForChild("RotatePart")

mouse.Move:connect(function()
    local x = mouse.X
    local y = mouse.Y
    local z = (x+y)/2
    part.Rotation = Vector3.new(x,y,z)
end)
The above worked much better that the above it would seem.

Here's a link that demonstrates this working.

There's an error with the above script as well though, and that's that it does not work with first person.

Moving the Part,


If you however wanted the part to follow the mouse position, you simply get the mouse, and then set the part as the mouses position. Like so,

-- LocalScript in StarterPack
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local part = game.Workspace:WaitForChild("RotatePart")

mouse.Move:connect(function()
    part.CFrame = mouse.Hit-- makes the rotation the mouses position
end)

However, the above has a problem with the part flying in your face. To fix this, check if the mouses target is the part. Like so,

-- LocalScript in StarterPack
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local part = game.Workspace:WaitForChild("RotatePart")

mouse.Move:connect(function()
    if mouse.Target ~= part then
        part.CFrame = mouse.Hit
    end
end)

I how that helped.

Good Luck!

Ad

Answer this question