Correct position on Y-axis

  1. What is the issue? I have a random generation script and I want parts to appear at the random position, but they tend to go partially inside the base part. I want to fix that and have the parts on the base part instead.

Here’s what it looks like right now, you can clearly see the part’s partially inside the base part:

image

Instead what it’s supposed to look like is this:

image

Pretty sure this requires some kind of a formula, so I suppose you guys are not gonna need any code.

1 Like

Positions are from the center of the part so if the part’s Y axis is up, you need to add half the Y size to the Y position.

2 Likes

Formula

local AlreadySetY = -- The Y Position Which Makes the Block go Underground
local FixedY = AlreadySetY+(Block.Size.Y/2)
1 Like

Here’s the updated code, it works but it’s still a little bit off the ground. How do I fix that?

local function WallsCreation()
	local RandomX, RandomZ = math.random(BackSide, FrontSide), math.random(RightSide, LeftSide)
	local RandomVector = Vector3.new(RandomX, 100, RandomZ)
	local Result = workspace:Raycast(RandomVector, Vector3.new(0, -100, 0))
	if Result then
		if Result.Instance == Carpet then
			local Wall = Assets:WaitForChild("Wall"):Clone()
			Wall.Parent = workspace
			Wall.Anchored = true
			Wall.Position = Result.Position + Vector3.new(0, Result.Position.Y + (Wall.Size.Y/2), 0)
		end
	end
end

1 Like

here, try this

local AlreadySetY = -- The Y Position Which Makes the Block go Underground
local FixedY = (AlreadySetY+(Block.Size.Y/2)) - (Ground.Size.Y/2)
1 Like

Still a little bit off:

local function WallsCreation()
	local RandomX, RandomZ = math.random(BackSide, FrontSide), math.random(RightSide, LeftSide)
	local RandomVector = Vector3.new(RandomX, 100, RandomZ)
	local Result = workspace:Raycast(RandomVector, Vector3.new(0, -100, 0))
	if Result then
		if Result.Instance == Carpet then
			local Wall = Assets:WaitForChild("Wall"):Clone()
			Wall.Parent = workspace
			Wall.Anchored = true
			Wall.Position = Result.Position + Vector3.new(0, Result.Position.Y + (Wall.Size.Y/2) - (Carpet.Size.Y/2), 0)
			Wall.Orientation = RandomOrientations[math.random(1, #RandomOrientations)]
		end
	end
end

try this

local AlreadySetY = -- The Y Position Which Makes the Block go Underground
local FixedY = (AlreadySetY+(Block.Size.Y/2)) - (Ground.Size.Y)
1 Like


local function WallsCreation()
	local RandomX, RandomZ = math.random(BackSide, FrontSide), math.random(RightSide, LeftSide)
	local RandomVector = Vector3.new(RandomX, 100, RandomZ)
	local Result = workspace:Raycast(RandomVector, Vector3.new(0, -100, 0))
	if Result then
		if Result.Instance == Carpet then
			local Wall = Assets:WaitForChild("Wall"):Clone()
			local FixedY = (Result.Position.Y+(Carpet.Size.Y/2)) - (Carpet.Size.Y)
			Wall.Parent = workspace
			Wall.Anchored = true
			Wall.Position = Vector3.new(Result.Position.X, FixedY, Result.Position.Z)
			Wall.Orientation = RandomOrientations[math.random(1, #RandomOrientations)]
		end
	end
end

if its a model so just do this

local pos = result.Position
local cf, size = Wall:GetBoundingBox()
local endPos = CFrame.new(pos.X, pos.y + (size.y / 2), pos.z)
local rot = RandomOrientations[math.random(1, #RandomOrientations)]
Wall:PivotTo(endPos * CFrame.Angles(math.rad(rot.x), math.rad(rot.y), math.rad(rot.z))

if not a model

local pos = result.Position
Wall.Position = pos + Vector3.new(0, Wall.Size.Y / 2, 0)
Wall.Orientation = RandomOrientations[math.random(1, #RandomOrientations)]
1 Like

Not a model, it’s a single part. I should’ve provided a picture with only one part

your raycast hits the top of the floor not the center
use this as your starting position

Vector3.new(result.Position.x, carpet.Position.Y, result.Positive.Z

1 Like

The position of parts are from the center of the part, I did what you said and this occured:

1 Like

i said use it as a starting position

take that then add on the Y, size of the part Y divided by 2 plus size of carpet divided by 2

start + Vector3.new(0, (wall.Size.Y / 2) + (carpet.Size.Y / 2), 0)

1 Like

Is your ground totally flat? In one picture, it looked like one edge of the block was closer to the ground than the other, or maybe it was just an illusion.

Yes, completely flat. whopper whopper

1 Like

I see you are placing them with Position
use PivotTo, and a cframe instead

Does this work for that?

Wall.Position = Result.Position + Vector3.new(0, Wall.Size.Y/2, 0)

I am sure you already said this in the
Wall.Position = pos + Vector3.new(0, Wall.Size.Y / 2, 0) that you mentioned earlier so it should work hopefully.

1 Like

Replace…

			---Wall.Position = Vector3.new(Result.Position.X, FixedY, Result.Position.Z)
			---Wall.Orientation = RandomOrientations[math.random(1, #RandomOrientations)]

with…

			local orientation = RandomOrientations[math.random(1, #RandomOrientations)]
			local cf = CFrame.new(Result.Position.X, FixedY, Result.Position.Z)
			Wall.CFrame = cf * CFrame.Angles(math.rad(orientation.X),math.rad(orientation.Y),math.rad(orientation.Z))

1 Like