How to Calculate NPC CFrame Using Body Scalers

While the title is somewhat confusing, let me explain. Using the Body Scaling NumberValue instances you’er able to add as a child to the Humanoid. How would I be able to calculate where to set the CFrame of the NPC where the feet are touching the ground after scaling? note: If you’ve ever played those Weight Lifting Simulator games, that’s the scaling I’m talking about.

I’m currently using this code:
I already know where the top of the spawn plate is and is being added to the script later

-- Character Generation Code to Pick a Random Size (inside a Module Script)
local randomSizeGen = math.random(65, 90) / 100
npc.Humanoid.BodyWidthScale.Value = randomSizeGen
npc.Humanoid.BodyHeightScale.Value = randomSizeGen
npc.Humanoid.BodyDepthScale.Value = randomSizeGen
npc.Humanoid.HeadScale.Value = randomSizeGen

-- Distance Calculation (inside the main NPC handler script)
local npcGen = require(game.ServerScriptService.Modules.characterGeneration) -- The module script
local npcTemplate, multi, sizeMulti = npcGen:pickCharacterType(temp)
-- Temp is the base NPC that is cloned
-- npcTemplate is the scaled NPC
distance = ((npcTemplate.HumanoidRootPart.Size.Y * 2) + npcTemplate.Head.Size.Y) / 2

A problem besides them floating like Gods is that NPCs also generate at varying levels of height from the spawn plate. note there is an invisible part currently level with the baseplate. I have checked 1000 times to make sure that’s not the problem

Current NPC Spawning Images

About a 1/4 stud gap:
npc
About 1 stud gap:
pc 2
Last, they spawn just barely into the ground (just feet):
ground

Well, the distance formula is:

(Origin - Destination)/2. In this case you add. Could that be the error?

By origin are you talking about the part of spawning the NPCs on?

Just any of the 2 places where you start your distance. It could be either really. You also want to calculate distance using .magnitude if you wanted it to be Vector3 values.

npcTemplate:SetPrimaryPartCFrame(CFrame.new(intersectionVector + Vector3.new(0, distance, 0)))

That’s the line where I set the CFrame of the NPC. intersectionVector is the Vector3 of a raypoint return. The raypoint is used to get the point of where to spawn it. Say the part I’m using to be the “spawner” is Size: 5, 1, 5 and Positon: 0, 0.5, 0(because the Size is 1 meaning the bottom is equal with the baseplate), the intersectionVector would return something like (3, 1, 4) or (2, 1 , 5) because the top of the “spawner” is 1. So I have to add that to the distance, where distance is the size of the character/2.
You’re able to get the size of the character model from HRP.Size.Y*2 + Head.Size.Y (thanks @ForeverHD). The problem is that the NPCs spawn at varying heights from the top of the spawner, so I’m not sure 1) where the HRP CFrame is calculated from, 2) why they spawn at varying heights and 3) if I’m even remotely close.

Distance formula? Where have you gotten this from?

Alright so I figured it out. I realized that the HumanoidRootPart position is 3/5 of the height of the model (without accessories). Knowing this, I changed the / 2 in the distance calculation to * (3/5). I also realized that the scaling wasn’t happening until the NPC was spawned so I had to multiply it in before it spawned. This is the new distance equation (that works):

distance = (((npcTemplate.HumanoidRootPart.Size.Y * sizeMulti) * 2) + (npcTemplate.Head.Size.Y * sizeMulti)) * (3/5)

According to the HipHeight documentation you can use this code to get the distance from the ground for an R15 humanoid

Height = (0.5 * RootPart.Size.Y) + HipHeight

or this for an R6 humanoid

Height = LeftLeg.Size.Y + (0.5 * RootPart.Size.Y) + HipHeight

then use the height to set the CFrame accurately above the ground by doing something like this

Character:SetPrimaryPartCFrame(CFrame.new(GroundPosition) + Vector3.new(0, Height, 0))

I’m assuming the HipHeight gets adjusted automatically when you perform scale edits.

1 Like

You know, thank you for being smart. I’m not the brightest person in the world so thank you. Just for that you can have the solution.
and for your note, most likely but I’m not going to test it cause either way I’ll have to multiply by 3/5 before it spawns to get the CFrame since I set it while it’s in RS and scaling doesn’t take effect yet