Can't make a part face the same direction as wall surface with raycasting (Normals)

Hello! yeah as the title says i can’t make a part face the same direction as the wall surface, i know this can be a stupid thing but i dont really worked with Normals, here is the code:

function RaycastNote(notePart)
	local rayP = RaycastParams.new()
	rayP.FilterDescendantsInstances = {notePart}
	
	RayRun = RunService.Stepped:Connect(function()
		local rayData = Workspace:Raycast(mouse.Origin.Position, mouse.Hit.Position * 20, rayP)
		
		if rayData then
			local targetPos = rayData.Position + rayData.Normal
			local lookAtRotation = CFrame.lookAt(notePart.Position, targetPos)
			
			notePart.CFrame = lookAtRotation
		end
	end)
end

Now this is what is happening:

Any help or tip would be appreciated

This post was quite confusing, but what I understood was that you wanted to have a part face the same direction as a wall that’s hit by a raycast. To do this, set the part’s LookVector to the Normal of the Ray.
If my understanding of your post was incorrect, please try to clarify exactly what you want to happen.

My bad then, but yeah something like that.
I tried doing this but didn’t work

notePart.CFrame.LookVector = rayData.Normal

and returned LookVector cannot be assigned to

as i said i havent worked with normals or lookvectors)

You’re not allowed to directly assign Part.CFrame.LookVector. Roblox only allows you to directly write to Part.CFrame, meaning you’d have to calculate a CFrame with the correct lookvector. One method I use to do this is setting the part’s CFrame to CFrame.new(part.Position,part.Position+Normal).

You also may just be able to set part.Orientation = Normal instead of constructing a new CFrame, so I guess try that first if you want. Not totally sure it works though.

It works well with the first solution, and i think the raycast is giving a bad result

local rayData = Workspace:Raycast(mouse.Origin.Position, mouse.Hit.Position, rayP)

Here is a video:

After opening studio I realized that note.CFrame = CFrame.new(ray.Position,ray.Position+ray.Normal) should be used to set both the position and the LookVector at the same time. Also, try setting up your raycast like this, where camera is equal to workspace.CurrentCamera:

local unitRay = camera:ViewportPointToRay(uis:GetMouseLocation().X,uis:GetMouseLocation().Y)
local ray = workspace:Raycast(unitRay.Origin,unitRay.Direction*20,params)
2 Likes

Hi i just tested it and works fine! Thanks for helping me on this! :slight_smile:

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