I was wondering how I could make a fence connect to another fence if it’s place next to one another?
Maybe raycast to the sides of the fence and see if the side is a fence? Then you can connect it.
Its really dependent on your code, and it would help to see your code a lot.
From what I can tell, you have some form of a grid system set up. You would have to look wherever you store grid data, and run a loop for all of the spaces around the fence you just placed. If one of which is another fence piece, you should run code to place a piece between them, which should look something like this.
- Define both fence posts
- Get the directional vector between them by subtracting post 1 from post 2, and calling .unit to normalize the vector.
- Get the position between the posts by finding the average of the posts’ positions (Post1.P+Post2.P)/2
- Get the Distance between the posts by using .Magnitude
- Make a new part and apply these properties where as unit is the orientation, the average distance is the position, and the magnitude is the size.
Sorry I couldn’t go into more detail as I didn’t have your code.
I’m kinda doing this just as a test, but it doesnt make the part the right size. This is testing with a single fence to a single fence:
End results:
local post1 = script.Parent.Post
local touchingParts = post1:GetTouchingParts()
if touchingParts[1].Parent.Name == "Fence" then
local post2 = touchingParts[1].Parent:FindFirstChild("Post")
if post2 then
print("Yes")
local dirVector = post2.Position - post1.Position
local unitVector = dirVector.Unit
local distanceBetweenPosts = (post1.Position + post2.Position)/2
local mag = distanceBetweenPosts.Magnitude
local newPart = Instance.new("Part")
newPart.Anchored = true
newPart.Orientation = unitVector
newPart.Position = distanceBetweenPosts
newPart.Size = Vector3.new(mag)
newPart.Parent = touchingParts[1].Parent
print(newPart.Parent)
end
end
Is the system you’re using voxel based? If so you could just check the voxel next to it see see if their is a fence there.
It’s not. It just uses a dummy model then sends a request to the server with the position based on the models primarypart. It just uses a simple 1x1 grid system to move the parts every 1 stud. Then places the model inside a folder where it holds all the items that are placed.
You’re best bet would be raycasting but that can be unreliable, maybe try changing your fence model or something.
I think a redesign of your base system would be better. Think of the fact that your placing system seems to have odd sized bases for your components. Think about how putting those into a game is going to give your players issues when they put items with what seems like 1x6 bases next to items with 4x6 bases. That’ll create problems adding other 4x6 based items, specifically when they try to rotate those items they won’t match up edgewise with others.
I’ve been trying to find a way to make this while allowing the most creativity with how they want to design their area. Anymore help on that would be greatly appreciated, could you maybe DM me in discord with more information on this? Discord: Gum#2461
If you play Lumber Tycoon 2 you may get a good idea of how to place smaller models. I found it to have a pretty sweet setup for building models from wall parts, fences, boards and such. It seems to use a more basic system of 1 stud, 2 stud and 4 stud parts, which work together well and you can place items on top of others so you aren’t limited to putting them on top of a baseplate.
In a case like yours I’d lean towards not putting a base on the fence and allowing players to put the fence where they want to on top of other items like grass and having the fence model meet up with another fence model beside it just by the shape of the boards.
The problem is that your dividing the distance between the parts by 2, then getting the magnitude of that vector. Remove the /2 off of distanceBetweenParts and add .Magnitude() for it to be the actual distance. And make a new variable for the position which should be (Post1.P+Post2.P)/2 --Thats where you use /2 to get the average position.