My issue is simple. I have a Model with an arbitrary height and a Part.
I want to place the Model of the potted plant where the bottom of the Model aligns with the bottom of the orange Part, but in a way where it is placed randomly within the confines of the orange Part. The Model has no PrimaryPart and it is important I maintain the CFrame of the orange Part so that the model inherits its orientation.
Here is how I am currently tackling the issue. "orange" is the orange Part and "pottedplant" is the potted plant Model. I am introducting randomness on the x and z axis to fit the model randomly with the boundaries of the orange box.
local att = orange.CFrame * CFrame.new(
Rnd:NextNumber() - 0.5 * orange.Size.X,
-orange.Size.Y/2 + pottedplant:GetExtentsSize().Y/2,
Rnd:NextNumber() - 0.5 * orange.Size.Z)
pottedplant:PivotTo(att)
The code mostly works, but it results in strange artifacts where the model is floating slightly above or is sunk slightly below the orange box.
Why are you using Random.new():NextNumber() if you want to get the accurate bottom normal id of the orange model? Just set the potted plant’s CFrame to the orange model’s CFrame and subtract the size Y difference from each other.
The Random.new() randomly shuffs the Model on the x and z axis. I don’t need the Model to be perfectly aligned with the orange block horizontally. I just need it to be vertically aligned, as I show in the second image.
More specifically, unless I am misunderstanding something with CFrame and the x and z arguments are interfering with the vertical placement of the Model, this line seems to be the problem:
The half sizes for both models are not relative for your CFrame operation to be vertically aligned, you need to specify the size Y difference between the two objects and remove that difference. That way, when your model is centered, you will get the space between the top and bottom of the bounds and move the model vertically downwards.
This does not seem to work. The models are still either too high or too small.
Again, the bottom of the model needs to be aligned with the bottom of the orange box. I do no care about where it exists on the horizontal axes.