Calculating the Y offset for a placement system

Hello! I started to work on a placement system(Some code is not mine) and I was facing issue with how to calculate the Y offset.

This is what I have so far

local function Round(Number)
		return math.floor((Number / ROUND_TO) + 0.5) * ROUND_TO
end

Mouse.Move:Connect(function()
		local TargetPosition = Mouse.Hit.p
		local RoundedTargetCFrame = CFrame.new(Round(TargetPosition.X), Round(TargetPosition.Y), Round(TargetPosition.Z))
		game:GetService("TweenService"):Create(Item,TweenInfo.new(.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{CFrame = RoundedTargetCFrame}):Play()
end)

If anyone could help me out that would be great! :grinning_face_with_smiling_eyes:

1 Like

Don’t round the Y axis to the grid scale. Move the model up by half it’s BoundingSize.Y. the 2nd value returned from GetBoundingBox is the bounding size.

I attempted this and these were my results:
EDIT: I want it to go on top of any part it detects if you know what I mean

local function Round(Number)
		return math.floor((Number / ROUND_TO) + 0.5) * ROUND_TO
end

Mouse.Move:Connect(function()
	local TargetPosition = Mouse.Hit.p
	local orientation, size = Item:GetBoundingBox()
	local RoundedTargetCFrame = CFrame.new(Round(TargetPosition.X),size.Y/2, Round(TargetPosition.Z))
		game:GetService("TweenService"):Create(Item.PrimaryPart,TweenInfo.new(.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{CFrame = RoundedTargetCFrame}):Play()
end)

heres a simple script for ya

local RS = game:GetService("RunService")

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local Part = Instance.new("Part")
Part.BrickColor = BrickColor.Random()
Part.Anchored = true
Part.Parent = workspace

Mouse.TargetFilter = Part
RS.RenderStepped:Connect(function()
	Part.CFrame = CFrame.new(Mouse.Hit.Position + Part.Size/2) 
	
end)

make sure you get the position you want it to towards then add the size of the object and divide by 2

thanks for the help! im going to create my own script:D

1 Like

The moving it up by half size bit needs to be an offset from the original Y value, not overwriting it.

local RoundedTargetCFrame = CFrame.new(Round(TargetPosition.X), TargetPosition.Y, Round(TargetPosition.Z)) * CFrame.new(0, size.Y / 2, 0)
1 Like

I did come to realize is this bug happens:
I think its because of the mouse position
(I dont have much experience with these systems lol)

its because the offset is being applied on all axis, moving you to the corner of the part each time.

you still need to apply the Y axis size half as an offset transform.

Part.CFrame = CFrame.new(Mouse.Hit.Position) * CFrame.new(0, Part.Size / 2, 0) 
1 Like

I had a feeling something would go wrong here, I rewrote it and used raycasting so I could get the normal of the part your mouse is pointing at, here ya go

now it will calculate the proper plane of every face

local RS = game:GetService("RunService")

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local Model = Instance.new("Model")
local Part = Instance.new("Part")
Part.BrickColor = BrickColor.Random()
Part.Anchored = true
Model.PrimaryPart = Part
Part.Parent = Model
Model.Parent = workspace

local IgnoreList = {Model, Player.Character}

local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = IgnoreList
RayParams.FilterType = Enum.RaycastFilterType.Blacklist

local MaxPlaceDistance = 2000


function RayCast()
	
	local MRay = Mouse.UnitRay

	local RayResult = workspace:Raycast(MRay.Origin, MRay.Direction.unit * MaxPlaceDistance, RayParams)
	
	if RayResult then
		local EndPos = RayResult.Position + (RayResult.Normal * (Model.PrimaryPart.Size*.5))
		Model:SetPrimaryPartCFrame(CFrame.new(EndPos))	
		
	end
	
	
end

game:GetService("RunService").RenderStepped:connect(function()
	RayCast()
end)


oh you are totally right, whoops!

thanks again both of you for the help!

1 Like

Glad we could help, if you have any questions make sure to ask!

1 Like

Hey actually I do have a question, if I wanted to insert a grid how could I pull this off?
EDIT: I don’t mean actually inserting a grid just getting it to move like it.

Worked out my own grid system:D