When I try to resize a part with a script, it expands/shrinks in both directions. For example, if I changed the Y value of a part’s size, it will shrink towards the middle from both directions, or grow from the middle. Can I change the “Anchor Point” of the part so that, for example the part will only grow upwards rather from both sides?
If you still don’t get what I mean, I’ll try to make a horrible visual demonstration here.
You can’t, but you can easily change the part’s position based on the size by changing its CFrame (or position if you prefer). You first need to get the difference of size that there was before and after the size change. Then you just need to change the position according to the difference.
local heightDifference = math.random(-5, 5)
newLand.Size = newLand.Size + Vector3.new(0, heightDifference, 0)
so for example, after doing this all id have to do is add
newLand.Position = newLand.Position - Vector3.new(0, heightDifference, 0)
Precisely, however you have to add the difference not remove it. Also, when you change the size, as you know the part scales from the center not the end of the part, so you also have to set the difference to its half first (Difference / 2
).
Ah, of course, that makes a lot more sense.
The dividing by two because it splits whatever size change in two and expands equally in both directions, correct?
And so the code should look more like this:
newLand.Position = newLand.Position + Vector3.new(0, heightDifference/2, 0)
Exactly. I’ll write down a simple code if you need a more accurate explanation:
local CurrentSize = Part.Size; -- We get the current size of the part, so we can calculate the difference later
local NewSize = CurrentSize + Vector3.new(0, math.random(-5, 5), 0); -- We calculate the new size of the part [only Y axis in this case]
local Difference = NewSize - CurrentSize; -- We calculate the difference
Difference /= 2; -- We divide the difference so the new position will be correct
Part.Size = NewSize; -- We set the part's size to the one we calculated before
Part.Position += Vector3.new(0, Difference, 0); -- We change the position of our part accordingly to the changes we made
Note that this will work with all three axis X,Y,Z. You can easily remove any of them by slightly changing the code.
Thanks a ton, the math part is always tricky, so I appreciate an in-depth explanation.
Also, is Difference /= 2 just a fancy way or saying Difference = Difference/2? I think I had seen someone do this before, isn’t it just a quality of life addition so you don’t have to type out your variable or whatever twice?
Yup. I usually use it to save up space so I can read my code more easily. But that’s a personal thing. And no problem, hopefully this helped you understand how to manipulate parts more easily.
Ok, so I tried the script, and while it works, there’s a further issue that I neglected to mention. It’s no problem with your code, rather a lack of context from me.
The point of this script is creating land- and I sometimes want to offset that land from the ground, so it isn’t just a flat plain. The script you set up for me works with something already on the ground- but what if I annex a new piece of land onto a piece of land that is taller than the others? The size will be adjusted accordingly, but since the Y value started off different, it will still be floating.
How can I fix this? Should I measure the magnitude between the Baseplate and the new land, and then divide it by two and subtract it from the Y value of the new land, or something? Would that even work?
Hmm, do you have a gif or image that can explain more accurately what you’re trying to obtain? Sorry but i’m not correctly understanding. From what I understood you’re trying to change a part’s size that is not on the ground, but rather on another part. But if that was the case the code I provided should work fine too.
I have an image, but that’s about all I think I can do.
As you can see, the parts are floating in the air. I’ll try to explain what I believe to be the root of the problem.
Basically, the way the land generation system works is it chooses a part and checks it’s sides to see if an existing part is there. If there isn’t any, it determines it is possible to add new land there, and it does.
Now, what I believe to be the problem, is that… well, if you ran this script 10 studs above the baseplate, it won’t resize it to touch the baseplate- the resizing isn’t relative to the baseplate, it just happens to work in that way. So, when you have it annexing new land off of a part that is flush to the baseplate, that works fine- but, when we add a new part, with new size, it’s position is changed slightly. Eventually, the change in position is so drastic that resizing the new land doesn’t stretch it all the way to the baseplate.
If none of that makes any sense, my new question is basically “how can I make it so the part always stretches to touch the baseplate”, if that makes sense.
Oh, so what you mean is, that part in the middle (shown in the image) is the result of it spawning in the middle of one of those two parts, meaning its Y position will be the same as that other part which results in it floating when changing its Y size. Then yeah, you can definitely use magnitude to fix this issue.
You could probably use the new Pivot methods… The pivot point of a model/part changes the point at which the model/part rotates and is positioned by.
Interesting, I’ve heard about these but don’t really know how to use them. Can you tell me more?
You can pre-set the BasePart | Roblox Creator Documentation for any model/part. So its pretty much exactly what your thinking in terms of changing its “AnchorPosition”.