Is it Possible to get the Normal Vector of a Surface?

I am trying to make a tool that creates and moves a part in the direction of a surface’s normal (i.e perpendicular) vector. The surface would be determined by a mouseClick. I have the tool itself along with the target detection completed; however, I need to make the part that is created move outwards from whatever surface is clicked (if I click the bottom of a brick, the new part would move downwards from it, and if I clicked the side of a brick at a 45 degree angle, it would come straight out of that side at a 45 degree angle as well, etc.)

How would I go about doing this (I would assume something to do with raycasting)? Is it even possible?

A similar scenario involves having a projectile come out of your player in the appropriate direction based on your rotation, and the orientation of the surface you’re standing on. This “Orientation” is difficult to obtain because your character doesn’t rotate according to the slant of the brick you’re standing on. How can I get that slant (assuming I know what brick the player is currently standing on)?

I understand this may be a very complex problem, but it would tremendously help my game development progress. Credit will be given where credit is due. Thanks in advance.

I’m not sure if this is of help but for your first question:

This could possibly be done by multiply CFrames, for example:

-- In this script 'object' is whatever object you would like to move
object.CFrame = object.CFrame * CFrame.new(0, 0, -1)

The code above will move the object 1 stud towards its front face (so if the front face is at a 45 degree angle then it will move towards the front face at 45 degrees.). This also works for all the others directions as in:

  • (0, 0, 1) is towards the back face
  • (0, 1, 0) is towards the bottom face
  • (0, -1, 0) is towards the top face
  • etc.

So in your case if you have found out what face they have clicked. Then you just have to work out which of the CFrames would be the opposite face to the one they have clicked and use the code above to move it towards that face.

Can you draw a picture of what you mean? Atm I can’t tell if you want a reflected vector i.e. if you hit at 45 it would come out at 135 degrees, it comes out as the surface normal, or that it should come out at whatever angle it hits it at (back to the origin)?

I am looking for it to come out as the surface normal. So if I click on a rotated brick, a part is created and oriented in the direction of the normal to the brick face I clicked on.

If you use the “Raycast” method, you can get a RaycastResult object which has a property called “Normal” which corresponds to the normal vector of the surface the ray intersected.

4 Likes