How do I get the center of a mesh instance?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By NocturnalWisp

This is more of an answer to a question I’ve been having that I thought I would share because I’ve been struggling on it for a couple of days and don’t want others to face the same issue.

I am attempting to get the center of a Mesh Instance for level loading purposes, but even using the Mesh Data Tool and averaging the vertices doesn’t seem to work.

The solution I found was to use MeshInstance.Mesh.get_aabb() which returns an aabb: AABB — Godot Engine (stable) documentation in English

I then used this aabb to calculate the center using this formula:

var aabb: AABB = mesh.get_aabb()
var center = aabb.position + aabb.size / 2

That code returns the center of the aabb of the mesh.

The quest to find this solution was a bit of a struggle because of how the get_aabb() differentiates itself from the custom aabb found in the inspector on the Mesh Instance. Maybe some documentation better explaining the difference could help?

Anyway, hope this helps whoever had the same problem!