Place a piece at the TOP of a position

Hi. I made some code that, based on the rotation and a given position, always places itself on top to prevent my piece from “falling” relative to its given position.

Code:

local function placeAbove(position, object)
	local objectCFrame = object.CFrame
	local objectSize = object.Size

	local rightOffset = objectCFrame.RightVector * (objectSize.X / 2)
	local upOffset = objectCFrame.UpVector * (objectSize.Y /2)
	local forwardOffset = objectCFrame.LookVector * (objectSize.Z / 2)

	local offset = Vector3.new(
		math.abs(rightOffset.X) + math.abs(forwardOffset.X) + math.abs(upOffset.X),
		math.abs(rightOffset.Y) + math.abs(forwardOffset.Y) + math.abs(upOffset.Y),
		math.abs(rightOffset.Z) + math.abs(forwardOffset.Z) + math.abs(upOffset.Z)
	)
	local newPosition = position + offset
	object.CFrame = CFrame.new(newPosition) * objectCFrame.Rotation
end

local raypos = workspace:Raycast(
	game.Workspace.Camera.CFrame.Position,
	game.Workspace.Camera.CFrame.LookVector * 1000
)
if raypos then
	placeAbove(raypos.Position, workspace.EjemploPieza)
	local p = Instance.new("Part",workspace)
	p.Position = raypos.Position
	p.Size = Vector3.new(.2,.2,.2)
	p.Anchored = true
	p.CanCollide = false
	p.Transparency = 0.5
	p.Shape = Enum.PartType.Ball
	p.Color = Color3.new(1,0,0)
end

Resultado:

error3
error2
error1

As you can see, it moves to the top corner, but I have no idea why. What I’m looking for is:
loquebusco

Keeping in mind that the rotation won’t always be perfect, so just halving the Y-axis size won’t work for me.

SORRY FOR MY ENGLUSH

2 Likes

Hi its still kind of unclear to me what you are trying to achieve. Do you want the center of the part to be above the ball, and still want it to be above the floor no matter the rotation?

1 Like

I want it to be above the given position regardless of the rotation. The ball is only there to indicate where it collided. Consequently, it’s the center where the piece should be, but always above the position. Can you explain this?

Okay i get what you mean. do you want it to look like this?

1 Like

I got a function that will calculate the object extents of a part, just see it as a part that fully wraps around the other part no matter the rotation. This function returns a vector3 for the size of the, lets say, hitbox. We set the position of the part to the desired location and then add the hitbox size to offset it on top of the location.

local function calculateObjectExtents(object)
	local objectCFrame = object.CFrame
	local objectSize = object.Size

	local corners = {
		objectCFrame * CFrame.new(-objectSize.X*0.5, -objectSize.Y*0.5, -objectSize.Z*0.5),
		objectCFrame * CFrame.new(-objectSize.X*0.5, -objectSize.Y*0.5, objectSize.Z*0.5),
		objectCFrame * CFrame.new(-objectSize.X*0.5, objectSize.Y*0.5, -objectSize.Z*0.5),
		objectCFrame * CFrame.new(-objectSize.X*0.5, objectSize.Y*0.5, objectSize.Z*0.5),
		objectCFrame * CFrame.new(objectSize.X*0.5, -objectSize.Y*0.5, -objectSize.Z*0.5),
		objectCFrame * CFrame.new(objectSize.X*0.5, -objectSize.Y*0.5, objectSize.Z*0.5),
		objectCFrame * CFrame.new(objectSize.X*0.5, objectSize.Y*0.5, -objectSize.Z*0.5),
		objectCFrame * CFrame.new(objectSize.X*0.5, objectSize.Y*0.5, objectSize.Z*0.5)
	}

	local minBounds = Vector3.new(huge, huge, huge)
	local maxBounds = Vector3.new(-huge, -huge, -huge)

	for _, corner in ipairs(corners) do
		local pos = corner.Position
		minBounds = Vector3.new(min(minBounds.X, pos.X), min(minBounds.Y, pos.Y), min(minBounds.Z, pos.Z))
		maxBounds = Vector3.new(max(maxBounds.X, pos.X), max(maxBounds.Y, pos.Y), max(maxBounds.Z, pos.Z))
	end

	return maxBounds - minBounds
end

local part = YOUR PART
local hitboxSize = calculateObjectExtents(part) 
-- set position and add the offset 
part .Position = POSITION + Vector3.new(0, hitboxSize.Y / 2, 0)
1 Like

Thanks, that’s exactly what I was looking for. Thank you so much for your help :smiley:

1 Like

Sorry to bother you, but don’t you know if your code can simulate the center of the part? I mean… look:

That is, from the point where it is grabbed (Object Position) it will be like the center that will be taken to calculate where it should be located.

Hi, my code is just for the absolute size of the part (hitbox). You’d have to add this kind of logic separate. Store the location when you grab the part and calculate that to object space and apply the offset to the position. For the offset you could do something like this:

local objectCFrame = part.CFrame
local localOffset = objectCFrame:pointToObjectSpace(RAY POSITION)
local newPosition = rr.Position * localOffset

local hitboxSize = calculateObjectExtents(part)
newPosition = Vector3.new(newPosition .X, offset logic + hitboxSize.Y / 2, newPosition .Z)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.