Surface Direction with Raycast

I am trying to look around and having some issue finding what I need, is there a way to have surface direction with Raycasting?

Yes, the RaycastResult will have a normal property.

I meant to say surface direction instead of surface detection, I have tried using the normal property but it isn’t seemingly doing what I need it to do.

What are your requirements for it?

I am trying to turn this segment of code into Raycasting instead of Mouse.Hit

local surfaceNormal =  Mouse.Normal
local surfaceDirection = surfaceNormal.Unit
local surfaceObjectNormal = Vector3.FromNormalId(surfaceNormal)
local surfaceWorldNormal = Mouse.Target.CFrame:VectorToWorldSpace(surfaceObjectNormal)

Ok. Do you need it to return an Enum.NormalId or do you want it to return the actual surface normal of what it’s hitting?

I believe I need to return an Enum.NormalId given the error I get when just replacing the mouse with my raycast result and such is

“Unable to cast Vector3 to token”

1 Like

Can you provide what your intended output is, or more context of the script you’re giving, because mouse.TargetSurface is an Enum.NormalId estimating the normal of where it is the mouse is hitting whereas RaycastResult.Normal is a Vector3 that is actually the normal.

The reason you’re getting this error is because, assuming Mouse is a RaycastResult, Vector3.fromNormalId requires an Enum.NormalId, a Vector3 is not an Enum.NormalId. I don’t think you need the surfaceObjectNormal variable or Vector3.fromNormalId at all, I think you should be able to just use surfaceNormal.

surfaceNormal is also always going to be a unit vector so you don’t really need to access its unit property

What I am working on is a system for a particle emitter I have to show up flat on all the surfaces it hits via rotating an attachment. To do this I need the surfaces and all of that. The raycast is raycasting to the center of the screen and there is a hitpart that goes to the surface the ray is hitting, so in the example here it is the ground.


It’s not positioning at all or rotating. If you need more explanation than go ahead given I don’t explain things that well lol

1 Like

Correct me if I’m wrong, but if you want to position something where your raycast hit, you can just do
RaycastResult.Position. I know you are using an attachment, but this post on visualizing rays may help you.

That is helpful, thank you

Something like this should work:

local mouseLocation = userInputService:GetMouseLocation() -- this returns a Vector2
local ray = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y) -- this returns a unit ray that is the equivalent of our mouse in world space

-- in the next line, we need to multiply ray.Direction by some number to give it a length greater than 1
local raycastResult = workspace:Raycast(ray.Origin, ray.Direction * 1000)

if not raycastResult then -- our ray didn't hit anything
	return
end

local normal = raycastResult.Normal -- this is the normal of the part we clicked the part in world space (so we don't have to do VectorToWorldSpace). this is a unit vector
local position = raycastResult.Position -- this is the position where our mouse intersected the part

-- finally, to get our final CFrame oriented properly,
local cf = CFrame.lookAlong(position, normal)

That worked! Thank you so much.

1 Like

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