How to properly calculate the Y offset for a placement system

  1. What do you want to achieve? Keep it simple and clear!

Im trying to make my placement system calculate the Y offset properly

  1. What is the issue? Include screenshots / videos if possible!

My problem is that. If an objects Y size is lower than 4. The model will be positioned above the model.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Ive tried fixing this myself but since im not the best with math it didnt go that well XD… Ive also tried looking here on the dev forum but cant find anything helpful

All the models you see has their Y size/pos set to 4 or higer expect the table

To give you a better understanding. Heres the placement system in action:

– // Code im using to calculate the CFrame

– // Cast a ray and return the position + surface normal

function ray(Pos) -- // Pos = Mouse position
	local Cam = game.Workspace.CurrentCamera:ScreenPointToRay(Pos.X, Pos.Y)
	
	local P = RaycastParams.new()
	P.FilterDescendantsInstances = {Plr.Character, game.Workspace.Terrain}
	P.FilterType = Enum.RaycastFilterType.Blacklist
	
	
	local RAY = game.Workspace:Raycast(Cam.Origin, Cam.Direction * 100, P)
	
	if RAY then
		return RAY.Position, RAY.Normal
	end
end

– // Convert the Position to object space and then calculate the grid. And the finally back to worldspace


function Convert_Cf(Base, Pos, Grid)  -- Base = Plot, Pos = Ray position, Grid = 4, 
	local ObjPos = Base.CFrame:ToObjectSpace(Pos)
	
	local Cf = Calc_Cf(ObjPos, Grid)
	
	local WorldPos = Base.CFrame:ToWorldSpace(Cf * CFrame.Angles(0, math.rad(Current_obj.PrimaryPart.Orientation.Y), 0))
	
	
	return WorldPos
end

— // And calculate the grid

function Calc_Cf(Vector, Grid) -- // Vector = The position, Grid = 4
	local Cf = nil
	if Current_obj.PrimaryPart.Size.X > 4 or Current_obj.PrimaryPart.Size.Z > 4 then
		Cf = CFrame.new(
			math.round(Vector.X / 2) * 2,
			math.round(Vector.Y / 2) * 2,
			math.round(Vector.Z / 2) * 2

		)
	else
		Cf = CFrame.new(
			math.round(Vector.X / Grid) * Grid,
			math.round(Vector.Y / Grid) * Grid,
			math.round(Vector.Z / Grid) * Grid
			
		)
	end
	
	return Cf
end

Any help would be really appreciated. BEcause i suck at math and cant really figure out how to solve this.

1 Like

I do not really understand what the problem is from looking at the video, can you explain again?

3 Likes

Alright ill try to explain a bit better.

When you hover over a models instance that has their Y size or position lower than 4. The model is placed some studs above it making a visible gap. I want it to properly calculate the position ontop of the thing the ray hit and not some studs above it.

2 Likes