Sorry if the title didn't make much sense, it's kind of tough to explain what I am trying to achieve.
I am working on a game that requires a placement system. Everything works fine, except for the boundaries of the player's plot.
At first, I calculated this by taking the position of the plot, subtracting that by the size of the plot divided by two. Then, I subtracted from that with the size of the object the player is trying to place down and divided that by two.
If this is hard to picture in your head, here is basically everything I explained above:
maxX1 = (workspace.Tycoons.Tycoon1.Base.Position.X - workspace.Tycoons.Tycoon1.Base.Size.X / 2) + ghost.HitBox.Size.X / 2 maxX2 = (workspace.Tycoons.Tycoon1.Base.Position.X + workspace.Tycoons.Tycoon1.Base.Size.X / 2) - ghost.HitBox.Size.X / 2 maxY1 = (workspace.Tycoons.Tycoon1.Base.Position.Z - workspace.Tycoons.Tycoon1.Base.Size.Z / 2) + ghost.HitBox.Size.Z / 2 maxY2 = (workspace.Tycoons.Tycoon1.Base.Position.Z + workspace.Tycoons.Tycoon1.Base.Size.Z / 2) - ghost.HitBox.Size.Z / 2
Probably not the best way to do it, but that's why I'm here in the first place :P!
While that works (sort of), if I ever had an object that was not in a perfect square shape, and I rotated it, then tried to place that object at the edge, it would think that it was not rotated.
Again, that's probably not explained very well so here is an image.
Here is an image of the object not rotated 90 degrees.
Here is also a GIF to better demonstrate.
As you can see, the object is being stuck two units from the edge. (One unit is 2x2 studs)
Now, here is the actual question: how do I fix this? What should I know, and what should I be doing?
If you need any more information, just ask in the comments.
So I'm still not entirely sure what you want, but I think this is the basic idea.
You want to have a grid placement system that properly accounts for size when you rotate your objects that are not perfectly square.
I suppose there are a few ways to do this, but probably the easiest way is just to flip the size x
and z
values depending on the rotation and then treat the placement like you normally do.
Here's a simple example where I do that:
local base = game.Workspace.Base; local part = game.Workspace.Part; local mouse = game.Players.LocalPlayer:GetMouse(); mouse.TargetFilter = part; local rotation = 0; game:GetService("UserInputService").InputBegan:connect(function(input, process) if (not process) then -- press r key to rotate if (input.KeyCode == Enum.KeyCode.R) then rotation = rotation + math.rad(90); end; end end); game:GetService("RunService").RenderStepped:connect(function(dt) if (mouse.Target == base) then -- rotate the x and z values local r = CFrame.Angles(0, rotation, 0) * part.Size; -- since it's size make sure components are positive local sx, sz = math.abs(r.x)*0.5, math.abs(r.z)*0.5; -- convert to object space for ease of calculation local p = base.CFrame:pointToObjectSpace(mouse.Hit.p); local size2 = base.Size*0.5; -- check to make sure the selected point has enough space for the object to fit on the base if (math.abs(p.x) + sx < size2.x and math.abs(p.z) + sz < size2.z) then -- place the object part.CFrame = CFrame.new(base.CFrame:pointToWorldSpace(Vector3.new(p.x, p.y + size2.y, p.z))) * CFrame.Angles(0, rotation, 0); end; end; end);