Hi all - I’m trying to solve a difficult problem and I’m looking here for ideas.
Looking for code that will produce the perfect camera angle of a monster given only the model of the monster.
Originally I was using the position of the mob’s primary part and the GetExtentsSize of the model, but I quickly discovered that roughly calculating the center of mass of the model and using that instead of the primary part position yielded much better results.
Now I’m looking for a way to get the “wieghted” extents size of a model, such that it focuses the camera on the highest concentration of mass, minimizing the extent to which tiny extruding parts offset the total size.
Here’s what I have so far:
local function getCenterOfMassOfModel(model)
local totalVotedPosition = Vector3.new()
local totalVotes = 0
for i,part in pairs(model:GetDescendants()) do
if part:IsA("BasePart") then
local center = part.Position
local mass = part:GetMass()
totalVotedPosition = totalVotedPosition + (center * mass)
totalVotes = totalVotes + mass
end
end
return totalVotedPosition / totalVotes
end
local centerOfMass = getCenterOfMassOfModel(entity)
local extents = entity:GetExtentsSize() -- this is the problem line
local camera = Instance.new("Camera")
local pos = centerOfMass + (extents * Vector3.new(0.5,0.1,-0.6))
camera.CFrame = CFrame.new(pos, centerOfMass)
camera.Parent = monsterButton
monsterButton.ViewportFrame.CurrentCamera = camera
This is the result:

It’s fine for a lot of mobs, but starts to get out of control with some of the crazier mobs in the game:

Suggestions for how to get a better extents vector are much appreciated. Other suggestions for how to improve the camera angle are also welcomed. Thanks!