How can i make a rust building system where you can snap foundations on all four sides?

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a rust building system where you can snap foundations on all four sides. So like, you can place a foundation on the front of the foundation, back, left and right. My foundation has attachments on all four sides named Floor Attachment.

Here is a video of how i want it to look like :

  1. What is the issue? Include screenshots / videos if possible!
    I don’t know how to do it.

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried YT and Devforum.

Please reply quickly as i need to get this done asap.

1 Like

I’ve never done anything like this before, so I can only make suggestions. What I would probably do is translate the mouse’s position into a world position (let’s call it x), and then take x and find the nearest part (let’s call it y). I’d then get the magnitude between x and y and see if it is within a reasonable snapping distance. If it is, then I’d do a magnitude check on all snapping sides of y, and use the closest one as my snapping point.

Of course, this is all a theory. There might be better ways to approach this, such as checking if the mouse is on the left, right, front, or back instead of checking with magnitude. That is all I can provide. Hopefully this helped just a bit.

Yea, i have done all of that, it snaps, its just the orientation. When it snaps the orientation is weird. Also i am using raycasting

Heres a picture of it not working

And here is a picture of it working :

It sometimes works and sometimes doesnt, here is the post where i got the code from : How to rotate part according to attachments orientation

Oh, so the problem is that the model is not rotating with the camera? If that is the issue, then you’ll just have to utilize the CurrentCamera’s CFrame (more specifically, the orientation), and set the model’s Y-axis orientation to be the same as the camera’s. That should theoretically align it correctly. I don’t know the exact calculations, so you’ll have to mess around with it on your own.

No, it does move its just that it doesn’t orientate the correct way sometimes for some reason, if it works once then it should work again, technically.

Heres the script i used:

–Put floor at attachment position via CFrame
local floorCframe = floor.CFrame *CFrame.new(ClosestAttachment.Position)

–Achieve goal rotation CFrame using axis from attachments
–We put emptyVector because we don’t want to add position into the CFrame from matrix
–Only rotation using the upvector and right vector
local emptyVector = Vector3.new(0,0,0)
local attachmentWorldUpvector = ClosestAttachment.SecondaryAxis
local attachmentWorldRightvector = ClosestAttachment.Axis

local goalRotationCFrame = CFrame.fromMatrix(emptyVector,attachmentWorldRightvector,attachmentWorldUpvector)

–apply rotation to floor rotation
floorCframe = floorCframe * goalRotationCFrame

–Then offset the position of the floor CFrame
floorCframe = floorCframe *CFrame.new(-floor.FloorAttachment.Position)

From what I can see, this uses world vectors. That means that the orientation will always be the same. As far as I’m aware, you have to make use of the CurrentCamera’s CFrame and get the Y angle (I think that is the UpVector if I am not mistaken) to be the same when not snapping. Unfortunately, I am not familiar with CFrame, so you’ll just have to either experiment on your own or wait for someone experienced to reply to this topic.

Oh ok, thanks for the quick replies

1 Like

From what I understood from your proposal, you want to make the bases automatically attach if they are very close.

i cant really help you if i dont have a base code or some example, but ill show you one

so here is a example of how you can do it

Alright, so this example looks for other nearby parts in a radius defined by the snapDistance variable(you can change it).
if it finds another base with a nearby anchor point, it will adjust the position of the current base to align with the nearby one. in addition, an event is used to detect when a base is placed and activate the docking.

basically, this code makes building structures easier by making the bases automatically snap to each other.

local snapDistance = 10 -- the distance 

local function snapFoundation(part)
    local attachments = part:GetChildren()
    
    for i, attachment in ipairs(attachments) do
        if attachment.Name:find("FloorAttachment") then
            local attachmentPosition = attachment.WorldPosition
            
            local nearbyParts = workspace:FindPartsInRegion3(
                Region3.new(attachmentPosition - Vector3.new(snapDistance, snapDistance, snapDistance),
                            attachmentPosition + Vector3.new(snapDistance, snapDistance, snapDistance)),
                nil,
                math.huge
            )
            
            for _, nearbyPart in ipairs(nearbyParts) do
                if nearbyPart ~= part and nearbyPart:IsA("Part") then
                    local nearbyAttachments = nearbyPart:GetChildren()
                    
                    for j, nearbyAttachment in ipairs(nearbyAttachments) do
                        if nearbyAttachment.Name:find("FloorAttachment") then
                            local attachmentVector = (attachmentPosition - nearbyAttachment.WorldPosition).Magnitude
                            
                            if attachmentVector <= snapDistance then
                                -- foundation to the nearby part
                                part.Position = part.Position + (attachmentPosition - nearbyAttachment.WorldPosition)
                                return
                            end
                        end
                    end
                end
            end
        end
    end
end

-- snap the foundation when it is placed
script.Parent.Changed:Connect(function(property)
    if property == "Parent" then
        snapFoundation(script.Parent)
    end
end)

This code is just an example, you will need to edit it as you like, hope it guides you to make your idea :slight_smile:

if you have any question, just reply!!

I have this done, its just the orientation that is weird, when it snaps the orientation is messed up

1 Like

modify the script to adjust the orientation of the foundation upon snapping.

you can use Cframe to match Y-axis

like this, for parts:

part.CFrame = CFrame.new(nearbyPart.CFrame.Position) -- Set position
    * CFrame.new(Vector3.new(0, part.Size.Y/2, 0)) -- Adjust height
    * CFrame.Angles(0, nearbyPart.CFrame.YRotation, 0)

one is for position, height and rotation

hope this can help you

Hey, its very close to working but it is floating a bit too high and i changed the height number it still isnt working. This is what it looks like :

maybe check the normals of the 2 sides your are snapping together and only snap if they are exactly negative of each other?

Omg thank you! It finally worked! I just adjusted the height and the attachments height and it worked! THANK YOU

1 Like

your welcome!, i hope that the project you are doing is a success

1 Like

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