0 votes

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: https://docs.godotengine.org/en/stable/classes/class_aabb.html#class-aabb-property-end

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!

Godot version 3.4
in Engine by (31 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.