[HELP] How to differentiate Wall from Floor or Ramp using RayCasting

I’m trying to get the angle from a part with the Mouse.Hit to place down a surface/part if the angle is equal to 180° or any but 90° since in my theory, 90° would be a wall. This would let me place down a surface/part on any desired angle within a range, except for walls.

It doesn’t have to be exactly an angle, but it could be a number that determines the rotation/angle of the hit part’s face.

Surface:
77ab2f63994c28f70fcaa9f0bba21bc0

Idea:

If you guys need more information, please tell me so that I can give it to y’all!

1 Like

I suggest instead of straight up using Mouse.Hit, but instead to use Raycasting. With Raycasting you can get the ‘Normal’ of the subject (the faces… all of them). I don’t really have much else to say but, Raycasting is the best bet for your goal.

That’s what I tried to do for a while, but now I have a problem

If you want to convert the raycast normal to an angle, you can multiply it by 90, 180, or 360. For your case it is best to multiply it by 90. Eithier by multiplying it strait by 90 or Vector3.new(90,90,90)

I think I found a way, but my current problem is that I don’t know how to let it wait to read the whole table to then do an action.

Here it detects if the part that was hit is equal to the ray’s part (to not confuse it with a floor/baseplate). look:

Then I need to see if the upVector, which will come out something like this:

a2e37c2d4f9a3d4f78377cb031028771

Contains a positive or negative 1, on its X or Z axis, signifying that it’s a wall. Therefore, in theory, I’d have to make something that checks if it’s not a wall, but if I put a bunch of or’s in my if, or I make a “for i, v in pairs()”, it will make the statement true before checking for the needed one.

115c55eb51ab3bbfc72ce4b3fd8a4d3d

I want to tell it that if the upVector is not equal to a negative or positive 1 in any vector, then run the logic.

The rotation will be acquired from detecting the touched part, I’ll get the rotation and put it on the surface.

Oh apologies, if you wanted to know if it’s a wall you can measure the angle between the worlds up vector Vector3.new(0,1,0) and the wall surface normal, if it’s perpendicular or around that range of 89 to 91 degrees it should most likely be a wall.

From VectorUtil module

local function angleBetween(vector1, vector2)
	return math.acos(math.clamp(vector1.Unit:Dot(vector2.Unit), -1, 1))
end

Otherwise another method to measure the wall is if the y component of the surface normal is minimal and close to zero as you noticed maybe between -0.1 and 0.1.

So… the way that I’m doing may be wrong? I mean… what I did is currently working, but I have a problem that if I’m in front of a wall, I won’t be able to place down a surface on the floor, but it at least puts it on ramps or surfaces if I’m not facing a wall.

Video Example:
robloxapp-20220101-1742149.wmv (3.2 MB)

script:

So how exactly can I do it your way? I don’t understand much how it works…

15a79e8724d0f5db5ba97d5a48ad2994

I noticed that I may not need the Vectors, and this form works wayyyy better than the last one… but the downside is that there’s a single time that the mouse.Target and the detected part don’t coincide, which is bad. This time is if you’re looking straight at a corner. The Mouse.Target and Ray will pick 2 different parts

Even though I fixed the corner problem, I just realized that by deleting the " (upVectorX ~= 1 and upVectorZ == 0 ", it doesn’t let me place the surfaces anywhere else but floors, not ramps.

fixed it, I detected a ramp by checking if the mouseTarget.Orientation.Z is bigger or smaller than 5 or -5

if (mouseTarget.Orientation.Z <= 0 or mouseTarget.Orientation.Z >=0) or ...(etc) then 
   -- do logic
end