Getting the localized normal of a part

I am casting a ray onto a block. The returning normal is of course global. By that, i mean it will be a vector that demonstrates the direction the normal facing. How could I get a more localized version? By that, I somehow use the lookvector of the part and the normal of the ray to determine which direction the normal should be facing.

This is the code I am thinking of that could do this, but I am wondering if there are any more optimized ways to do this.

function normal_to_lookvector(cframe: CFrame, normal: Vector3)
	if normal == Vector3.new(0,0,-1) then
		return cframe.LookVector
	elseif normal == Vector3.new(0,0,1) then
		return -cframe.LookVector
	elseif normal == Vector3.new(1,0,0) then
		return cframe.RightVector
	elseif normal == Vector3.new(-1,0,0) then
		return -cframe.RightVector
	--etc
	end
end

I appreciate any help.

Clarification may help, I’m not sure what you’re trying to accomplish.

If you already weren’t aware:
Normals describe a perpendictular vector to a surface, each axis is constrainted between -1 to 1 because it’s a directional vector, not positional.

Example Image

What needs to be “localized”?

I’m not really understanding your code snippet because cframe.LookVector is a world-space (global) forward vector of the cframe, while in your written post you are interested in finding the local vector.

You can get the object-space (local) vector of the cframe by using CFrame:VectorToObjectSpace:

cframe:VectorToObjectSpace(normal)

For example, cframe:VectorToObjectSpace(normal.LookVector) should give you approx CFrame.new(0, 0, -1)

If you want to get the world-space vector from the cframe you can use VectorToWorldSpace instead.

I’m sorry, I may have specified what I wanted wrongly. I’m thinking of changing the normal vector depending on the direction a part is facing. I’ll try out your suggestion and tell you how it goes, though I’m going to bed now. Thanks for your reply!

that’s weird, the normal in your diagram (and in a test i just conducted) did have the normal the way i wanted it. for some reason, the normal in my main project was not behaving the same way due to the change in some code. i made this post without knowing that.

however, i do still have that persisting issue in my code.
this is a part of my grid based object placement system.

local object_offset: Vector3 = (ray.Normal * (model_region_space_size[vector_to_axis(ray.Normal)]) / 2)

--model_space_region_space_size is a size vector that shows a region in which the given instance takes up. it takes the very min vertex of the part, and vise versa, and creates a region. this variable is only the size part of that  region
--vector_to_axis turns a vector into a string. e.g Vector3.new(0,1,0) -> "Y"

i’m having an issue where when ray.Instance is rotated by >90 degrees, that axis is off (check vector_to_axis learning comment).

here is an example with my problem more visualized