How can I make a part go to the mouse position with the orientation of the mouse.Target part?

Im making something where a part follows the position of the mouse if there’s a mouse.Target. However, Im having a difficult time finding out how I would rotate it accordingly. Could I get any lead/guidance on how to do it?

what I mean:
image
I want the circle to be on the wall instead of inside it

Thanks.

2 Likes

If you’re wanting the part to face against the wall, then I suggest you learn raycasting as it would be the perfect solution to this :slightly_smiling_face:

I do know about raycasting, but how would I apply it to this?

Position the part at the raycast.Position and orient it to face the raycast.Normal

You would do something like this:

YourPart.CFrame = CFrame.new(YourPart.Position, Mouse.Hit.p+Normal)

Not sure if you need to use Target or Hit

You can shoot the ray from the mouse’s position to the mouse’s target and do something like

if ray then
   local hit, pos, normal = ray.Instance, ray.Position, ray.Normal
   YourPart.CFrame = CFrame.new(YourPart.Position, YourPart.Position + normal) 
end

Lemme know if I did something wrong as I didn’t test it inside studio yet :joy:

Should be positioned at the ray’s hit position actually

if ray then
   local hit, pos, normal = ray.Instance, ray.Position, ray.Normal
   YourPart.CFrame = CFrame.lookAt(pos, pos + normal) 
end

My bad lol, I just wrote the script without even testing it :sweat_smile:

hmmm. Doesn’t seem to be quite working. Maybe I did the raycast wrong?

local ray = workspace:Raycast(Player.Character.Head.Position + Vector3.new(2,0,0),Mouse.Hit.Position)
			if ray then
				local hit, pos, normal = ray.Instance, ray.Position, ray.Normal
				Module.CFrame = CFrame.lookAt(pos, pos + normal) 
end

You need to use a unit vector for your direction that points towards mouse.Hit.Position

local raycastRange = 10 --10 studs long raycast

local origin = Player.Character.Head.Position + Vector3.new(2,0,0)
local direction = (Mouse.Hit.Position - origin).Unit * raycastRange

local ray = workspace:Raycast(origin, direction)
1 Like

behavior is a bit strange currently

image
image

Connection = RunService.RenderStepped:Connect(function(DT)
			local raycastRange = 100 --10 studs long raycast

			local origin = Player.Character.Head.Position + Vector3.new(2,2,0)
			local direction = (Mouse.Hit.Position - origin).Unit * raycastRange

			local ray = workspace:Raycast(origin, direction)
			if ray then
				local hit, pos, normal = ray.Instance, ray.Position, ray.Normal
				Module.CFrame = CFrame.lookAt(pos, pos + normal) 
			end
		end)
	end
1 Like

Edit: got it to work.

everything you said was right, but the problem was you can’t exactly do this on a circle. (since the “front” side isnt the face of the circle). What I did was made a cube part and welded the circle to the front of the cube. Thanks!

1 Like

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