Issues with calculating Y position for target object

I am having trouble calculating the Y position of the top of a target object. Here is my current implementation of CalculateYPosition

Heres Images:


image

It does work normal when placing on top:
image
image


function CalculateYPosition(pos)

	local posY = pos.Y
	local primaryPartSize = PrimaryPart.Size
	
	return math.ceil( posY + primaryPartSize.Y/2) 
end

1 Like

How do you want it to work? What is pos?

1 Like

the pos parameter is the ray cast hit result’s position

well asking about how I want it to work is by just don’t change the pos if it is touching the sides of an object or moving the part on top of the object like it is used to work in normal object

1 Like

I still don’t quite understand, sorry. Do you want it to work like Minecraft’s block placement works? So the part will be placed aligned and flush with whichever face you clicked?

yes, do you know how we can achieve something like that ?

I answered a similar question here which might be helpful to you:

Dk if that’s what i want, actually am having with the y positioning

Hers by entire script, am using oop for now so I can later play around adding some add methods etc

local Placement = {}
Placement.__index = Placement


local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local Camera = game.Workspace.CurrentCamera

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()



local GRID_SIZE
local ITEM_FOLDER

local Object 
local PrimaryPart
local PlacedObjects


local PosX
local PosY
local PosZ

function CalculateMouseRayCast()
	local mousePos = UserInputService:GetMouseLocation()
	local mouseRay = Camera:ViewportPointToRay(mousePos.X,mousePos.Y)
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = {Object}
	
	local rayCastHitResult = workspace:Raycast(mouseRay.Origin,mouseRay.Direction * 50,raycastParams)
	
	return rayCastHitResult
end


function CalculateYPosition(pos)
	local posY = pos.Y
	local primaryPartSize = PrimaryPart.Size
	
	return math.floor(posY +  primaryPartSize.Y/2 + 0.5) 
end

function SnapPosition(x)
	return math.floor(x / GRID_SIZE) * GRID_SIZE + GRID_SIZE/2
end

function CalculatePosition()
	local rayCastHitResult = CalculateMouseRayCast()
	
	if not rayCastHitResult then return end
	
	local raycastHitInstance = rayCastHitResult.Instance
	
	if rayCastHitResult and raycastHitInstance then
		PosX = SnapPosition(rayCastHitResult.Position.X)
		PosY = CalculateYPosition(rayCastHitResult.Position)
		PosZ = SnapPosition(rayCastHitResult.Position.Z)
	end
	
	
end


function SetPosition()
    if PlacedObjects and  Object.Parent == PlacedObjects then
        CalculatePosition()

        Object:PivotTo(CFrame.new(PosX,PosY,PosZ))
    end
end

function Placement.New(gridSize : number,itemFolder : Folder)
    local self = setmetatable({},Placement)

    GRID_SIZE = gridSize
    ITEM_FOLDER = itemFolder

    self.GridSize = gridSize
    self.ItemFolder = itemFolder

    return self
end

function Placement:Configure(itemName : string,placedObjs : Folder)
	Object = ITEM_FOLDER:FindFirstChild(itemName):Clone()
	PrimaryPart = Object.PrimaryPart
    PlacedObjects = placedObjs

    if not Object then return end

    Object.Parent = PlacedObjects
end

RunService:BindToRenderStep("Input",Enum.RenderPriority.Input.Value,SetPosition)

return Placement