Hey guys, so I have a weird problem with my grid-snapping system. It’s working just fine for 15x15 structures but any time I try to use 20x20 structures (or really any multiple of 10) the grid is off-line.
Here’s the snippet of my code that controls the placement/snapping
--"plate" is the part that the structures are being placed upon
--"rotation" is a multiple of 90
--"item" is the structure being placed
local ray = Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction*1000)
local hit,rPos,normal=workspace:FindPartOnRayWithWhitelist(ray,{plate});
local x,z=math.floor(rPos.X/5+0.5)*5,math.floor(rPos.Z/5+0.5)*5;
pos=Vector2.new(x,z)
item:SetPrimaryPartCFrame(CFrame.Angles(0,rotation,0)+Vector3.new(pos.X,plate.Position.Y+(plate.Size.Y/2)+(item.PrimaryPart.Size.Y/2),pos.Y));
This is for a 5x5 grid, however 10x10, 20x20, etc. structures are being offset by 2.5 studs (being placed in the center of the grid cells) while it’s working perfectly fine for structures that are 5x5, 15x15, etc.
Does anyone happen to know what’s going on here? Any help is greatly appreciated
The part spans the entirety of the model (as you can see from the outline below the model when placing it), it’s just offset from the grid which leads me to believe it’s a problem in my math somewhere.
Raycasting is the easiest way to fetch the mouse’s hit position with the baseplate without anything else interfering. If I were to use mouse.Hit or something like that, I would have to use mouse.TargetFilter that only supports one model at a time.
--calculate your grid snapped position like this instead
local placingAreaPart = --you need to place the part that you are placing items on here
local model =--put the model you are placing here
local rotation = --insert the rotation here of the model
local modelSize = CFrame.fromEulerAnglesYXZ(0, rotation, 0)*model.PrimaryPart.Size
local size,size2
modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))
size = Vector2.new((placingAreaPart.Size * Vector3.new(-1,0,0))).magnitude, (placingAreaPart.Size * Vector3.new(0,0,-1)).magnitude)
size2 = (size - Vector2.new(modelSize.x, modelSize.z))/2
x = math.sign(rPos.x)*((math.abs(rPos.x) - math.abs(rPos.x) % 5) + ( Size2.x% 5))
z = math.sign(rPos.z)*((math.abs(rPos.z) - math.abs(rPos.z) % 5) + ( Size2.z% 5))
If you look above I am using that method currently, however the problem seems to stem from my grid being an odd number (5). I already follow all the grid guidelines in my primary parts & structures.
I’m going to try to add an offset for any multiples of 10 and see if maybe that’ll solve my problem. I’ll update this reply w/ my results.
Ok great! If you are not taking rotation into account then you might wan’t to take a look at the answer to the post I posted. My placement post (not required).
One other thing though, I would recommend you to not use ray casting for the use of ignoring other objects on the plot.
I instead just get the tycoon objects folder/model and set that as the mouse’s target filter.
There is a way around this. I know I said this in a reply, but I will explain how I do this more in depth since this is simpler. I usually have my plot organized like this:
If you set the mouse’s target filter to the PlacedObjects folder, then all the objects will be ignored!
But I also want to be able to filter out your character & other players’ characters and anything else that might be in the way. Basically it’s easier for me to set a whitelist than it is to set a blacklist.
also i would recommend you use this tutorial its where i learned about placement systems for a game im working on its very good: Creating A Furniture Placement System