Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

help me with rotating a part towards my mouse?

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

✓ Accepted answer

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 )

1 comment

0

Thanks it works now :)

This discussion is preserved as it was. New answers, comments, and votes are closed.