Rotating a part around a origin point to face the mouse?

Alright, give me a few seconds and i’ll make a template in studio. Could you give me the proportions of each object? Preferably the faces, and such.

1 Like

If you’re working with mesh parts, you could remove the mesh ids and send me the model. Would help me get the right proportions with ease.

1 Like

Here it is.
gear.rbxm (62.2 KB)

1 Like

I got it to rotate around the origin point using
model.Pointer.CFrame = model.Pointer.Part.CFrame * CFrame.Angles(0, 0, math.rad(i * 2)) * CFrame.new(1, -10, 0).
Now all thats left is making it point to the mouse cursor

That’s the fun part I had that working a while ago. However the angle between mouse pos and center pos is giving me troubles, I have it rotating on the right hand side, but reflecting it seems almost impossible :joy:

https://gyazo.com/3cf57b75e752938a736d19235431e5e8
heres what i did, im only a few steps away from completing it
seems like i have the same problem as well

Here’s how far I’ve come
https://gyazo.com/23a7e3d9ffb8948d43ce443430421441
The angle is not quite right but almost there.

1 Like

I hope you find a solution soon :smiley:

Actually, couldn’t you just multiply the CFrame by CFrame.Angles(0, 0, math.rad(90))?

Close, however I did figure it out I think.

local function ConvertToVec2(Vector)
	local screenSize = Camera.ViewportSize
	Vector = (Vector2.new(Vector.X, Vector.Y) - screenSize/2) * (2/screenSize)
	return Vector3.new(Vector.X, -Vector.Y, 0)
end

game:GetService("RunService").RenderStepped:Connect(function()	
	local CenterPos = ConvertToVec2(Camera:WorldToScreenPoint(Center))
	local Position = ConvertToVec2(UIS:GetMouseLocation())

	local Difference = Position - CenterPos
	local Angle = math.atan(Difference.X/Difference.Y)
	if Position.Y > CenterPos.Y then
		Angle = Angle + math.rad(180)
	end
		
	Clock.Pointer.CFrame = CFrame.new(Center) * CFrame.fromAxisAngle(Vector3.FromNormalId(Enum.NormalId.Front), Angle) * CFrame.new(0, -Clock.Pointer.Size.Y/2, 0)
end)
2 Likes

Sorry about all the print statements, used it for testing and forgot to remove.

1 Like

That’s a lot more complicated than I thought. Thank you so much!

I’d just like to point out, that the Center variable is a WorldPosition of an Attachment. You can change it as needed.
https://gyazo.com/1643c2706aaf0329f933e61d138dbd2d
Also I took the liberty to make it Relative to the screen size etc.

3 Likes

I’m getting quiet a few errors on my side.
I assume Camera is workspace.CurrentCamera? It throws Unable to cast Instance to Vector3 if i set the camera like that.

local UIS = game:GetService(“UserInputService”)
local Camera = workspace.CurrentCamera
local Center = Model.BigWheel.Attachment.WorldPosition

Those where the extra variables I had. The reasoning behind me using an Attachment was for a fixed location relative to the big wheel except it had a Position of 0, 0, -10 studs placing it 10 studs infront of the BigWheel

Alright, it works perfectly now. Thank you so much. I would have never figured it out myself!

1 Like

No problem, I had fun working on it in all honesty, probably would have never done something like that otherwise so thanks for posting the question.

1 Like

Ah, I found a bug.
Sorry to let you know but this method doesnt work if you look away from it. Then the rotations starts to bug out.
I’m using viewport in conjuction with this so its a bummer : (

1 Like

That’s indeed a shame, could you show me a visual of the bug?