How do I make a placement system that aligns with the mouse target surface?

Hello! So I’ve been trying to make this placement system for my game and, I want to make it align with Mouse.TargetSurface. How can I accomplish that?

Disclaimer: I just need the Math for it, Don’t actually write an entire placement system just for this one forum post, Alright?

2 Likes

Raycast returns a normal which is perpendicular to the surface hit. If you place based on the position of the part (center), offset by size of the parts, oriented based on the surface normal, you should get perfect placement.

I’ve already thought of this but, how do I get the size axis to use based on the normal?

if you are using blocks, it is easy. Say the blocks are size 1, then from the middle to the side is 0.5, and then again another 0.5 to the middle of the next block. For irregular shapes… my brain hurts!

You could probably procedurally figure this out for every building block, but it might be easier to figure these values out and add them as attributes to the block (or use OOP, etc).

I am not using blocks, But using walls, And other things

There are a few threads on this sort of calculation. This one tries to get the size of the surface, which you are going to need for irregular shapes, but to get there, it describes how to calculate the distance to the surface. With weird size shapes (walls) you are also going to need to figure out how to align with the edge of the adjoining part.

How to get a surface’s size with raycasting - Help and Feedback / Scripting Support - DevForum | Roblox

Ohhh wait I think I figured it out already, I’ll just create an array that has all the axes like this

local axes = {
Enum.NormalId.Top = "Y";
Enum.NormalId.Bottom = "Y";
Enum.NormalId.Front = "Z";
Enum.NormalId.Back = "Z";
Enum.NormalId.Left = "X";
Enum.NormalId.Right = "X";
}

Woops! Forgot to add square brackets, But that’s alright, I’ll fix it.

1 Like