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
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?
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