Part Face Mouse Direction

I want to make the part face the mouse from its vector2 direction, making the part orientation rotate (x axis) to face the part


I tried to make the part do its thing but the part just goes haywire

part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(math.rad(Mouse.X), math.rad(Mouse.Y), 0)

Why not use mouse.Hit.Position and use only one axis (either X or Z in this case)

i did so but when theres no part to be hit for the mouse to register, it does not stare at anything

anyways i solved it after awhile and got the behavior which i wanted

local HitCF = Mouse.Hit
		local Origin = Part.Position

		local CF = CFrame.new(Origin, HitCF.Position)
		local RX, RY, RZ =  CF:ToOrientation()
		local newRY = 0
		
		local directionVector = Origin - Mouse.Hit.Position
		local dotProduct = CFrame.new(Origin).LookVector:Dot(directionVector)

		if dotProduct > 0 then
			newRY = -180
		elseif dotProduct < 0 then
			newRY = 0
		end
		
		local orientation = Vector3.new(math.deg(RX), newRY,math.deg(RZ))
		Part.Orientation = orientation
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.