You can get the positions of the corners of the part using the part’s CFrame and either Vector3s or CFrames of half the part’s size on each axis.
The *
operator on CFrames will shift the left operand by the right operand according to the left operand’s orientation and position. In other words, it gets the world space of the right operand assuming it’s in the local space of the left operand.
An example: part.CFrame * Vector3.new(part.Size.x/2, 0, 0)
will give you the point in the center of the right face of the part, regardless of how the part is rotated.
part.CFrame * Vector3.new(part.Size.x/2, part.Size.y/2, -part.Size.z/2)
would give you the top right front corner. We can repeat this for all corners to get all corner positions.
The following function returns an array of Vector3s corresponding to every corner of the given CFrame and Size:
local function getCorners(cframe, size)
return {
cframe * Vector3.new(-size.x/2, -size.y/2, -size.z/2),
cframe * Vector3.new(-size.x/2, -size.y/2, size.z/2),
cframe * Vector3.new( size.x/2, -size.y/2, size.z/2),
cframe * Vector3.new( size.x/2, -size.y/2, -size.z/2),
cframe * Vector3.new(-size.x/2, size.y/2, -size.z/2),
cframe * Vector3.new(-size.x/2, size.y/2, size.z/2),
cframe * Vector3.new( size.x/2, size.y/2, size.z/2),
cframe * Vector3.new( size.x/2, size.y/2, -size.z/2),
}
end
The following lets you pass in a part:
local function getPartCorners(part)
return getCorners(part.CFrame, part.Size)
end
The *
operator also works for shifting a CFrame by another CFrame. part.CFrame * CFrame.new(part.Size.x/2, 0, 0)
works just as well as part.CFrame * Vector3.new(part.Size.x/2, 0, 0)
, you just get a CFrame at the position with the part’s rotation instead of a Vector3.
CFrame *
Operator
You can check if all of the corners of the part are (not) in the player’s view, but this requires 8 times as many checks. I feel like if you have to do too many of these, it’ll become a performance issue. You can probably get away with using some sort of buffer space around the screen if you are doing this with a lot of parts. You’ll only get an “estimate” of if it’s on the screen then, but it’s better than low performance.
What are you trying to check if parts are on the screen for?