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

help me with rotating a part towards my mouse?

Asked by
Kryddan 261 Moderation Voter
9 years ago

I am trying to make a part rotate towards my mouse. Kinda like a compass where the red arrow always pointing towards the earth's magnetic north pole. So what i want is that the part rotates so it points towards my mouse. The only problem is that it also rotates in the x-axis when i only wants it to rotate in the y-axis.

If you need me to exlpain better then tell me.

I have tried this

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = workspace.Part


while wait() do
    part.CFrame = CFrame.new(part.Position,mouse.hit.p)
end

I have also tried

    part.CFrame = CFrame.new(part.Position.y,mouse.hit.y)
    --instead of 
    part.CFrame = CFrame.new(part.Position,mouse.hit.p)

But it gives me a error: bad argument #1 to 'new' (Vector3 expected, got number)

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You want to point towards mouse.Hit.p, but you want that position to not make it point vertically.

You want mouse.Hit.p to have the same y as part.Position (but the same x and z)

We can make that pretty easily:

local lookTo = Vector3.new( mouse.Hit.p.x, part.Position.y, mouse.Hit.p.z )

You could also do it with vectory math:

local lookTo = mouse.Hit.p * Vector3.new(1, 0, 1) + mouse.Hit.p * Vector3.new(0, 1, 0)

THen it's just using lookTo:

part.CFrame = CFrame.new( part.Position, lookTo )
0
Thanks it works now :) Kryddan 261 — 9y
Ad

Answer this question