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 :
What is the issue? Include screenshots / videos if possible!
I don’t know how to do it.
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.
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.
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.
–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.
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