Raycast surface normals issue

Hey! I have an issue reading the surface normals when using Raycasting.

Im using this sphere, and supposedly it has normals as it shows in blender.

After exported it and imported it into Roblox Studio. Im Raycasting, hitting the sphere, and the hit is successfull but normals is nil, heres the code:

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local rayOrigin = script.Parent.Position
local rayDestination = game.Workspace.Sphere.Attachment.WorldPosition

local rayDirection = rayDestination - rayOrigin

while true do
	local hit, position, normal = game.Workspace:Raycast(rayOrigin, rayDirection, raycastParams)

	if hit then
		warn(hit.Instance) -- This is showing the sphere instance in output
		warn(normal) -- This is nil
		if normal then
			local inclination = math.deg(math.acos(normal.Z))
			print(inclination) -- print the inclination angle
		end

	end
	wait()	
end

What should I do to read the angle inclination of a surface?

This returns a RaycastResult, not tuple, so with your current code do this:

local result = game.Workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if result then
local hit, position, normal = result.Instance, result.Position, result.Normal
end

Nope, thats not the normal I want to get, thats the Result.Normal, and its about the object orientation, I want to get the Surface normal, which as far as I understand can be obtained like this:
Its the angle inclination of the surface at hit point

-- Define the origin point and direction of the ray
local origin = Vector3.new(0, 0, 0)  -- Start at the origin
local direction = Vector3.new(1, 0, 0)  -- Go in the positive X direction

-- Perform the raycast
local hit, hitPos, hitNorm = workspace:Raycast(origin, direction)

-- Check if the ray hit an object
if hit then
  -- Print the surface normal of the hit point
  print(hitNorm)
else
  -- Print a message if the ray did not hit an object
  print("The ray did not hit any objects.")
end

That’s the old style of raycasting using FindPartOnRay. He’s actually correct here since you’re using Raycast.

Ok, but, then Im getting the same reading no matter where the hit point is, no matter where I hit the sphere its always the same reading. Only when I rotate the sphere I get different readings. Is that the surface angle inclination then?

My assumption is that raycasting treats it like a cube. Is this a MeshPart? I’m not sure if raycasts work properly with meshes. Can you create a little part at the ray’s end point to see if it’s hitting a cube face or a mesh face?

Here’s the thing, @Dyzody told me the opposite:

I’m really not sure what he’s talking about. All parts have normal vectors and you definitely can’t get three separate returns out of Raycast. Are you familiar with what a surface normal is?

Also what are you trying to do? If it’s important we might be able to find a workaround.

Nope, not really, I know almost nothing about normal maps.

I’ve never used Raycast trying to get those 3 values, I always used it like @SubtotalAnt8185 suggest, but this time Im trying to get the angle inclination of the hit point on a sphere.

For example, do a Raycast, find the inclination surface angle and the place a part in there that is aligned 90° in relation with the sphere. Similar to the first image I sent from blender, where little lines are aligned 90° like coming out of the sphere.
image

RaycastResult.Normal is what you’re looking for. It does just this.

Make sure that your sphere in Roblox has CollisionFidelity set to PreciseConvexDecomposition or Hull.

2 Likes

Totally true, using that Normal angle make it works, thank you so much. I was confused cause ppl told me about the “different surface normal” I dont know why I didnt tried with the Result.Normal in first place

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