Surface normal help

So I’m trying to make a spray paint bottle right now, and since my game is shift-locked I’m going to raycast in front of the player’s camera and use the intersection position of a potential hit to see what the player is trying to spray. An invisible part with a decal on its front surface would then be put at the intersection position. One thing I need to know though is how to align the invisible part with the spray decal on it to the surface it hits. For example, if a player was looking forward and the raycast hit a part standing upright (like a wall), and it was its left surface, then the part with the decal on it would still face toward you, like this:

Also as well as complicated rotated parts like this:

I think this has something to do with surface normals, but I have no idea how to use them or how to accomplish this. Can someone help?

3 Likes

I am not an expert at cframes but what I think you would do is set the Cframe of a new transparent part with a decal on it to CFrame.New(hitPosition, surfaceNornal). This makes a CFrame that is positioned at the hit point facing the surface normal. (Or it might be CFrame.new(hitPosition, hitPosition + surfaceNormal) ) Good luck! Other people probably know more about CFrames than me if neither of those constructors produce the correct CFrame. Good luck!

1 Like

The third value that FindPartOnRay() returns, is the surface normal of the area that was hit. You can use this to orientate your spray paint part’s orientation to match the orientation of the hit surface.

Some example code:

local hit, pos, normal = game.Workspace:FindPartOnRay(ray) --normal is a vector that points in a certain direction
	    
local SprayPaintPart = game.ServerStorage.SprayPaintPart:Clone()
SprayPaintPart.Parent = workspace
SprayPaintPart.CFrame = CFrame.new(pos, pos + normal) --calculate the LookAt Vector by adding the hit position to the normal directional Vector

The “normal” value is a directional Vector3 with a magnitude of 1, otherwise known as a unit Vector, which basically means that it points in a certain direction to the extent of one stud.
Some information on directional Vectors:

For example, you could get the LookVector of a CFrame, and use it to extent a part in that direction.

LookVector = Part.CFrame.LookVector
Part.Position = Part.Position + LookVector * 10 --this would move the part forward 10 studs, forward being the front face direction of the part

--there are many uses for directional Vectors, but this is just an example
11 Likes

Okay, I’ll try this. Can I get some explanation of how this works, if you don’t mind? I want a better understanding so I can feel confident if I have to do this type of stuff in the future.

For example, how does this orientate the part?

What is the surface normal vector, exactly?

I did some marking up and explaining of my original comment. Hopefully it clears stuff up.

Tell me if you come across any other problems.

Thanks for the info! I already know about LookVectors though, I just needed some understanding on surface normals. I’ll mark your post as the solution for now, hopefully this works

1 Like

Just tested, works perfectly. Thanks!

1 Like