How would I correctly place these pads?

Hello,
I am wrapping up my game (My Island Resort) and I am currently experiencing a bug that I have not been able to solve. I have tried raycasting so far, and I haven’t been able to come up with anything else.

The problem is that since I am making a tycoon, the pads need to be placed on the ground so the player can easily step over them. The problem is, that the pads depend on the position of the actual item being spawned, and end up spawning in the correct positions but with weird Y’s. (As seen here: https://i.gyazo.com/424ebdc7a2e882c4159eabf040b38f67.mp4)

My script snippet that is the problem:

local cf = CFrame.new(table.unpack(ref:GetAttribute("Offset"):gsub(" ",""):split(","))) -- this is the seralized cframe converted to object space based on the plot
	clone:SetPrimaryPartCFrame(self.Plot.Island.Origin.CFrame * cf:Inverse()) -- sets the place of the pad
	clone:SetPrimaryPartCFrame(clone:GetPrimaryPartCFrame() * CFrame.Angles(0, 0, math.rad(90))) -- rotates it so it isn't upside down or whatever
	
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {self.Plot.Buildings, self.Plot.Pads}
	params.FilterType = Enum.RaycastFilterType.Blacklist
	local result = workspace:Raycast(clone.PrimaryPart.Position, Vector3.new(0, -100, 0), params) -- this is the raycasting

	if (result.Position ~= nil) then
		print(result.Position)
		clone:SetPrimaryPartCFrame(CFrame.new(clone.PrimaryPart.Position.X, result.Position.Y / 2 + clone.PrimaryPart.Position.Y / 2, clone.PrimaryPart.Position.Z)) -- this is my problem, the Y gets messed up or whatever and idk what happens.
		clone:SetPrimaryPartCFrame(clone:GetPrimaryPartCFrame() * CFrame.Angles(0, 0, math.rad(90))) -- this resets so i do it again
	else
		print('uh oh')
	end

Current:
image

Expected:

I’ve done a lot of research on Google with the proper keywords and the DevForum. Thanks for reading.

1 Like

Maybe change the line where you had problem to this?

clone:SetPrimaryPartCFrame(CFrame.new(clone.PrimaryPart.Position.X, result.Position.Y + clone.PrimaryPart.Size.Y / 2, clone.PrimaryPart.Position.Z))

Basically the result.Position.Y is the position where your raycast hit, and you add the half thickness of your pad to make it not sink in the ground.

EDIT: Sorry you may have to change the clone.PrimaryPart.Size.Y a bit based on which axis it is that controls the thickness of your pad sorry.

1 Like

Thanks for the reply!

It gets really close but isn’t on the ground quite yet.

image

1 Like

Find the intersection with the ground as translateCf

          local boundingCF, boundingSize = self.PreviewModel:GetBoundingBox()

            self.PreviewModel:SetPrimaryPartCFrame(translateCF * CFrame.new(0, (boundingSize.Y * .5) + .1, 0))

This will move the model to that position then up by exactly half its bounding height. If your rootpart is the bottom part you can swap boundingSize with that rootpart’s size.

1 Like

Could you elaborate on what “translateCF” is?

You can just write something like this:

local BoundingBoxCenter,BoundingBoxSize = clone:GetBoundingBox()
clone:SetPrimaryPartCFrame(CFrame.new(clone.PrimaryPart.Position.X, result.Position.Y + BoundBoxSize.Y / 2, clone.PrimaryPart.Position.Z))

The bounding box is basically the smallest rectangular prism that surrounds the model. It returns the center of the model and the size of the model.

EDIT: Sorry I edited the code a bit. I forgot the Model:GetBoundingBox() returns a tuple.

The same problem is happening.

local translateCf = CFrame.new(raycastResult.Positon)

Maybe try changing the code to match the axis that controls the thickness of the pad? For example, if it’s the x-axis that’s changing the thickness of the pad, change the code to this:

clone:SetPrimaryPartCFrame(CFrame.new(clone.PrimaryPart.Position.X, result.Position.Y + clone.PrimaryPart.Size.X / 2, clone.PrimaryPart.Position.Z))

If it’s the z-axis that’s changing the thickness of the pad, then it would be this:

clone:SetPrimaryPartCFrame(CFrame.new(clone.PrimaryPart.Position.X, result.Position.Y + clone.PrimaryPart.Size.Z / 2, clone.PrimaryPart.Position.Z))

Try all of these and see if one works.

1 Like

@InfinityDesign
Still produces this result:

@DargoA it works!

I really appreciate you helping me with this. You have a nice rest of your day :grinning_face_with_smiling_eyes:

(Final product)

1 Like