Hi! Today I’m going to show you how to place a part on top of another part. This works even if the you change the size of the part you want to put on top of another part.
(NOTE: This is only for people new to Vector3 and is not targeted to people with much knowledge about it. )
(NOTE 2: This is a short tutorial and also my first topic. I would likely appreciate it if you give feedback. )
This is the code that I made which I’m about to explain:
local partA = game.Workspace:WaitForChild("PartA")
local partB = script.Parent
partA.Position = partB.Position + Vector3.new(0,(partB.Size.Y/2 + partA.Size.Y/2),0)
Rough sketch of partA
and partB
:
So basically what this does is setting the partA
(The part you want to put on top of another part) 's position to partB
(The part below the partA) 's position then adding a Vector3
to it. As you can see, the Vector3
's Y axis is the only one with a value. This is because we’re only talking about the Y axis a.k.a the top and the bottom of the part. The value in the Y axis is the sum of the half of the Y of partB
's size and the half of the Y of partA
's size, which is 5. Why do we need to divide the Y axis of the size of both parts before adding them? This is what happens when you don’t divide them:
As you can see there’s a large gap between the two parts. That’s because we don’t divide the Y axis of the size of both parts before adding them. This is what happens when you do it correctly:
Thanks for reading it and hope it helps! Feel free to give feedback.