How do I match my parts CFrame to another parts orientation?

Hi, I’m trying to make my models cframe match the part my mouse is pointing to, however, I am stuck with rotations. Anyone can help?

Video to show:

Code:

function MouseMoved()
	if Item then	--	If there is a Note selected
		local MouseTarget = Mouse.Hit
		local MouseObj = Mouse.Target
		local TweenItem = Services.TweenService:Create(ItemCloned.PrimaryPart, 
                TweenInfo.new(.1), {Position = MouseTarget.Position})
		TweenItem:Play()
	end
end
2 Likes

You would want to use raycasting and get the Normal.

1 Like

Not entirely sure how this helps?

Getting the surface normal of what your ray hits will return the direction of the parts hit face
image
Similar example of how you would utilize this where they use it for Bullet Holes

P.s you would need to have a whitelist so it only goes on surfaces you want and doesnt stack forwards or on characters

So my code is supposed to look a little something like this?

function MouseMoved()
	if Item then	--	If there is a Note selected
		local MouseTarget = Mouse.Hit
		local MouseObj = Mouse.Target
		
		local raycastResult = workspace:Raycast(ItemCloned.PrimaryPart.Position, MouseTarget.Position, RaycastParams.new())
		
		if raycastResult then
			local TweenItem = Services.TweenService:Create(ItemCloned.PrimaryPart, TweenInfo.new(.1), {CFrame = CFrame.new(MouseObj.Position, MouseObj.Position + raycastResult.Normal)})
			TweenItem:Play()
		end
	end
end

	local RayCastWhiteList = RaycastParams.new()
	RayCastWhiteList.FilterType = Enum.RaycastFilterType.Blacklist
	RayCastWhiteList.FilterDescendantsInstances = {Player.Character} 
-- for your scenario you want every player and every note thats been placed, 
--so you would want to also store your notes in a folder which
-- i'm assuming you already do (in workspace)

local Mouse = Player:GetMouse()

 -- rayresult = raycast ( Origin , Direction * Distance , Params )
local rayResult = workspace:Raycast(HRP, (Mouse.Hit.p - HRP) * rayRANGE, RayParams)
if rayResult then
		local hit = rayResult.Instance
		local normal = rayResult.Normal
        bullethole.CFrame = CFrame.new(Point, Point + normal) 

What is HRP and the Point variables supposed to be?

Oh my bad, Point is meant to be Mouse.Hit.P, and HRP = Character.HumanoidRootPart

Awesome, working, however feels buggy:

Try making the origin the cameras position instead

With Mouse.TargetSurface you can calculate the normal where the mouse touches the part. Then with some math you can calculate a target CFrame for your animation. This is quite a bit smoother.

Remember that you still need to restrict which parts can touch or what happens if you point it at the sky. If this gets complicated you should use Raycast to calculate the normal.

function MouseMoved()
	if Item then	--	If there is a Note selected
		local MouseTarget = Mouse.Hit
		local MouseObj = Mouse.Target
		local normal = MouseObj.CFrame:VectorToObjectSpace(Vector3.FromNormalId(Mouse.TargetSurface))
		local newcframe = CFrame.lookAt(MouseTarget.Position, MouseTarget.Position + normal)
		local TweenItem = TweenService:Create(ItemCloned.PrimaryPart, 
			TweenInfo.new(.1), {CFrame = newcframe})
		TweenItem:Play()
	end
end

Mouse.Move:Connect(MouseMoved)

1 Like