Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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, etc.)

How would I go about doing this? 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.

2 comments

0

This is not complex at all, and I know you didn't attempt this scripting wise yet, but I'll write a script for you anyway.

0

I was going to accept your answer, but I did in fact try to do this many times. You didn't have to assume that I didn't, and that was rude.

1 answer

The way to get details from an object that is being clicked on would be to use raycasting. To do it using the UserInputService (which is recommended for tools), is to get a unit ray (a ray with a length/magnitude of 1) from the camera to the mouse position in 3D space by using Camera:ScreenPointToRay

then multiplying that by about 1000 studs to create a new ray and using the new ray with Workspace:FindPartOnRay to get information about the part it hits, the hit position, and the surface normal.

function getNormalFromClick(x,y)
	local ray1 = Camera:ScreenPointToRay(x, y, 0)
	local ray = Ray.new(ray1.Origin,ray1.Direction * 1000)
	local part, hitPos, norm = workspace:FindPartOnRayWithIgnoreList(ray, game.Players.LocalPlayer.Character)
	return norm
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local surfaceNormal = getNormalFromClick(input.Position.X,input.Position.Y)
		--do stuff with the normal
	end
end)

If you need help integrating this into what you have, then let me know, otherwise you can just mark this as accepted.

This discussion is preserved as it was. New answers, comments, and votes are closed.