How would I tweak this to account for rotation?

(Part.Position + Vector3.new(math.Random(-Part.Size.X, Part.Size.X), math.Random(-Part.Size.Y, Part.Size.Y), math.Random(-Part.Size.Z, Part.Size.Z)))

This code chooses a random position within a part, but it doesn’t account for rotation so even if the part is rotated it will act the same as if it was not rotated.

1 Like

Part.CFrame * CFrame.new(math.random(-Part.Size.X/2, Part.Size.X/2),math.random(-Part.Size.Y/2, Part.Size.Y/2),math.random(-Part.Size.Z/2, Part.Size.Z/2))

Adding to the position will be in World space, while multiplying by a cframe will be in Local space

2 Likes

Part.Size.X, is the full width of the part. You want half that range in each direction if you want a point inside the part, no?

When you post-multiply your part CFrame by a CFrame that is pure translation, as Starception has done, the translation is in the part’s local coordinate space. So Part.CFrame * CFrame(x,y,z) is the same as:

Part.CFrame + x*PartCFrame.RightVector + y*Part.CFrame.UpVector - z*Part.CFrame.LookVector

Note the subtraction of the LookVector, since LookVector is in the -Z direction.

When you pre-multiply, i.e. CFrame.new(x,y,z) * Part.CFrame, the translation is in world space, like your original code.

3 Likes

It works just fine without having to divide each range. I don’t know if it’s the same with CFrames though

1 Like

Hey @Starception. Thank you so much haha, your method worked and you taught me something I didn’t know before.

1 Like

No problem!
I completely drew a blank and @AllYourBlox’s reminded me that I needed to divide the size by two. Otherwise, it could go farther out past the part’s boundaries.

I edited my post with the proper divisions.

1 Like

Oh alright, I’ll get on that too

1 Like

It might be doing what you need, but it’s not picking a point inside the part, that’s what I meant. I wasn’t sure what you actually wanted. The chance your random point is inside the part is only 12.5%, 1 out of 8 times.

2 Likes

I divided the sizes by two and it’s a lot more accurate. Thank you!

1 Like