How to get a surface's size with raycasting

Hello devs! I’m new to this forum and I want to make a question.
The title says all, I want to make so when raycasting it returns the surface’s size.
Any way to do it?
Thanks :smiley:

1 Like

So you wanna shoot a ray and then it prints the bricks size or something?

Not really the brick size.
You see, when raycasting it returns: hit, position, normal, material.
I want to make it return the size of the surface, for example:
If the ray hits the back of the brick, and it is 5 studs long, then the ray should return the value 5 as a Vector3

Try if this works (untested):

local part, _, normal = workspace:FindPartOnRay(example)

local normalSize = normal * part.Size

The size will be represented as a Vector3 but you can convert it into a Vector2 for plane size instead by having only the X and Z values I believe.

1 Like

I think I know what you mean, it can be a bit complex but I’ll try my best to explain.

Let’s say…

This is your first raycast, this ray will hit the surface of your brick.

FIRST, we gotta get the surface normal, so we can take the next step.

Last time I did this I think I did it with this.
Althought you may want to remove or change the CFrame.Angles part if it doesn’t give the correct rotation,
I used this for paint splatters and such on walls/wedges/meshparts, etc.

CFrame.new(position, position + normal) * CFrame.Angles(math.rad(90),0,0)

Doing this should kinda give you this…

We just calculated a CFrame that we can use!

Now we just gotta add maybe 2 - 4 more rays to that?

Make sure you use a whitelist that only has said part in the array because we’re now gonna use rays to calculate the size of the surface.

Of course, you gotta offset the start position of the rays.


This is the next thing I do, make a offset from the surface position we calculated, do 2 - 4 more rays (you have to do it for both X and Y if you want the surface size in 2D instead of 1D.)

Calculate magnitude between the rays that we did from top/left/bottom/right

aaaand there ya go, now you have the surface size if I’m correct.

Hope you’re getting the idea I’m giving you here, if something is incorrect feel free to correct me fellow forum members.

3 Likes

Yes! The way you explained it I understood perfectly, thanks!

1 Like

Based on your approach, I wonder if this would be an alternative solution:

local sizeMultipliedByNormal = normal * part.Size/2
local normalSide = part.Position + sizeMultipledByNormal
local normalSize = ((normalSide + sizeMultipledByNormal.X) - (normalSide - sizeMultipledByNormal.X)).Magnitude

I am not sure if it’s correct though. But if it is, I believe this may already be shortenable to:

local surfaceSize = normal * part.Size

And since size can’t have negatives the result would require math.abs. I guess this is meant to be equivalent to the size of a plane based on normal rather than the magnitude of a side from normal.

1 Like

I don’t think magnitude ever returns negative numbers though, I’ve never had that before.

Also your approach seems pretty interesting, haven’t tried if that works.
I don’t use a raycast’s surface normal often but I might because they’re very useful in many ways actually.

I don’t mean as in magnitude returns negative numbers I mean for my approach if you multiply the normal by size. (normal could probably contain negative numbers)

Is it possible you can list some useful ways surface normals can be used in? I actually never have any case that lets me use them.

Paint splatters, bullet holes, hovering vehicles that adjust to how steep the ground is, foot prints, building systems that let you place things on walls/wedges/ceilings
anything on surface or that needs to be adjusted to surface really.

1 Like

The first one is literally what I’m trying to do haha!

1 Like

What do you mean raycast from the other side? What if the part was incredibly thin and the raycast won’t register? Where would I raycast it from and to?
@mjvnq2, did you ever solve this problem? I’m really struggling right now as this is the ONLY POST ON DEVFORUM that talks about finding a surface’s size.
I’ve been working on a building system for at least a month now and the boundary/clamping part is where I’m stuck.
:pray::pray::pray: Still looking for answers

How do you “make a offset” from the surface position? What if the part was like this
image
How would you discover the up and right vectors?


How will we discover the up and right vectors? We only have the normal vector of the surface.
Just so you guys know this is what I mean by discovering the up and right vectors

Without up and right vectors and only the look vector, it’s impossible to determine the size of the area. I need someone with incredibly advanced knowledge of CFrames, Vectors, Geometry, and Roblox… hint hint… Egomoose

You make the offset relative to the part of course, you use the part’s CFrame rotation as a base.

How does one “make the offset relative to the part”

Ray’’’’’’’’’’’’’’’’’’’’’'s orientation