Hello. I'm working on a model moving and placing function for a tycoon. However, when I go to place a model, the model is not centered and this causes the player to be able to place his model off the grid a little bit. I'm not sure what the problem is buts frustrating me and is a decent sized chunk of code. If someone wouldn't taking a look and seeing what the problem is it would be much appreciated.
If you need to see the problem go to my place and experiment with placing models from your inventory on to your baseplate on the edges https://www.roblox.com/games/1656142905/Rocky-Mountain-Mining-EARLY-ACCESS
while placing do if Mouse.Target then if Mouse.Target:IsDescendantOf(GetTycoon()) and Mouse.Target.Name == "BasePlate" then local TycoonBase = Mouse.Target local TycoonBaseSizeX, TycoonBaseSizeZ local HitboxSizeX, HitboxSizeZ if Rotation % 180 == 0 then TycoonBaseSizeX, TycoonBaseSizeZ = TycoonBase.Size.x, TycoonBase.Size.z HitboxSizeX, HitboxSizeZ = Hitbox.Size.x, Hitbox.Size.z else TycoonBaseSizeX, TycoonBaseSizeZ = TycoonBase.Size.x, TycoonBase.Size.z HitboxSizeX, HitboxSizeZ = Hitbox.Size.z, Hitbox.Size.x end local TycoonTopLeft = TycoonBase.CFrame * CFrame.new(Vector3.new(TycoonBase.Size.x/2, 0, TycoonBase.Size.z/2)) local BaseAngleX, BaseAngleY, BaseAngleZ = CFrame.new(TycoonBase.Position, (TycoonBase.CFrame*Vector3.new(1, 0, 0))):toEulerAnglesXYZ() local VectorRotation = CFrame.Angles(BaseAngleX, BaseAngleY, BaseAngleZ) local RoundByThreeX = RoundByThree(math.floor(Mouse.Hit.p.x - TycoonTopLeft.x)) local RoundByThreeZ = RoundByThree(math.floor(Mouse.Hit.p.z - TycoonTopLeft.z)) if RoundByThreeX < -TycoonBaseSizeX + HitboxSizeX then RoundByThreeX = RoundByThreeX + HitboxSizeX end if RoundByThreeZ < -TycoonBaseSizeZ + HitboxSizeZ then RoundByThreeZ = RoundByThreeZ + HitboxSizeZ end local VectorComponent1 = CFrame.new(Vector3.new(TycoonTopLeft.x, Mouse.Target.Position.y, TycoonTopLeft.z)) local VectorComponent2 = CFrame.new(Vector3.new(RoundByThreeX, Mouse.Target.Size.y/2, RoundByThreeZ))*VectorRotation local VectorComponent3 = CFrame.new(Vector3.new(-HitboxSizeX/2, Hitbox.Size.y/2, -HitboxSizeZ/2)) local NewVector = VectorComponent1*VectorComponent2*VectorComponent3 local RegionVectorMin = NewVector * CFrame.new(Vector3.new(HitboxSizeX/2, -Hitbox.Size.y/2, HitboxSizeZ/2)) local RegionVectorMax = NewVector * CFrame.new(-Vector3.new(HitboxSizeX/2, -Hitbox.Size.y/2, HitboxSizeZ/2)) local Hitboxes = game.Workspace:FindPartsInRegion3( Region3.new( Vector3.new( math.min(RegionVectorMin.x, RegionVectorMax.x), math.min(RegionVectorMin.y, RegionVectorMax.y), math.min(RegionVectorMin.z, RegionVectorMax.z) ) + Vector3.new(0.1, 0, 0.1), Vector3.new( math.max(RegionVectorMin.x, RegionVectorMax.x), math.max(RegionVectorMin.y, RegionVectorMax.y), math.max(RegionVectorMin.z, RegionVectorMax.z) ) - Vector3.new(0.1, 0, 0.1) ), nil, 100 ) local ModelColliding = false for i, Hitbox in pairs(Hitboxes) do if Hitbox.Name == "Hitbox" and not Hitbox:IsDescendantOf(Visual) then ModelColliding = true end end if not ModelColliding then LastPosition = NewVector * CFrame.Angles(0, math.rad(Rotation), 0) end Visual:SetPrimaryPartCFrame(LastPosition) end end wait() end --end of placing
So, without looking at the code itself, and just testing it in-game. It looks like the way you have the model displayed is causing it. It is always projecting out of a certain part of the mouse and is always facing a certain way, you could try to center it more into the mouse by using a certain part of the model as the "center" of it. Or you could have the game check if it's on the grid or not, and if it's not on the grid pop up and error saying it couldn't be placed and place it back in the players inventory.