- Home /
Setting the tensor manually
This error has been annoying me for a very long time, and I can not find a proper answer to it. It is the really annoying error:
Actor::updateMassFromShapes: Compute mesh inertia tensor failed for one of the actor's mesh shapes! Please change mesh geometry or supply a tensor manually!
UnityEngine.MeshCollider:set_sharedMesh(Mesh)
A lot of people are saying to turn the mesh collider into a box collider, but that is not a possibility here. So, I try and do what it suggests right before the line that is throwing this error.
//This updates the collisionMesh based on the animations and what not
UpdateMesh();
// not sure why this is necessary, but it is
meshCollider.sharedMesh = null;
//The meshRigidBody is the meshCollider.rigidBody
meshRigidBody.inertiaTensor = Vector3.zero;
meshRigidBody.inertiaTensorRotation = Quaternion.identity;
// set the collider mesh.
// THIS is the line that is throwing the error.
meshCollider.sharedMesh = Instantiate(collisionMesh) as Mesh;
Yet that does absolutely nothing. How am I suppose to set the tensor manually?????????? (Note: I don't even need the physics that is going on with the tensor stuff, but we need a RigidBody)
Answer by nullstar · Feb 20, 2013 at 10:09 AM
Have you tried setting the inertia tensor to something other than a zero vector? A zero inertia tensor only makes sense for a kinematic body which is perhaps why Unity is still attempting to calculate it from the volume anyway. You could calculate an approximate inertia tensor by imagining a bounding volume around your mesh and using one of the equations from here -> "List_of_moment_of_inertia_tensors". Note that Unity's inertia tensor vector is the diagonal elements of the matricies in the given link.
Doctor .. "(Note: I don't even need the physics that is going on with the tensor stuff, but we need a RigidBody)"
Physics does not exist without the moments of inertia.
WHAT IS IT YOU ARE TRYING TO DO?
if you simply set the freezes on the rigidbody, that eli$$anonymous$$ates all such calculations.
It just depends what you're trying to do. WHY are you using a rigidbody?
There's probably an extremely easy solution. Cheers
I have tried setting the inertiaTensor to the one vector, and that didn't change anything.
Rigid bodies help with something to do with animations (I can get the exact reasons if you want). Either way, I know for a 100% fact that I need them.
The rigid bodies are kinematic, yet the error is still thrown.
I don't know what will happen if I freeze all of the transformations, but I will give that a try and get.back to you.
From the Unity docs:
If you don't set intertia tensor from a script it will be calculated automatically from all colliders attached to the rigidbody.
Does your object start off with a rigidbody and a valid collision mesh attached in the editor? If that's the case then Unity is probably trying to calculate the inertia tensor from the attached collider during initialisation, before your script which manually sets the tensor is run. In this case you should remove the collider from your object in the editor and only add it in script after the manual setup of the tensor has occured.
Freezing the transformations did nothing. The model is in an asset bundle, but I think it might have the default mesh collider at startup. The line above is when the mesh collider is changed each and every time. But if Unity decides to test for a tensor every time because one wasn't set at a very specific moment, they need to state where. In the Awake, the mesh collider that was assigned to it (public) isn't initialized yet, so I can't set the tensor there. And setting it in the start function does nothing. Where else could I set it??
Answer by Mike McFarland · Mar 13, 2013 at 06:44 PM
I had a situation where the behavior of plane colliders (only one side collides) was desired on parts of a rigid body which also had other valid colliders. What ended up being effective was disabling the collider behaviours on the prefab, then before the colliders were needed capture the already calculated tensor, cache it, enable the colliders, and then set the values afterwards.
Vector3 planeInertiaTensor = topPlane.rigidbody.inertiaTensor;
Quaternion planeIntertiaTensorRotation = topPlane.rigidbody.inertiaTensorRotation;
foreach (MeshCollider collider in planeColliders)
{
try
{
collider.enabled = true;
}
catch { }
}
topPlane.rigidbody.inertiaTensor = planeInertiaTensor;
topPlane.rigidbody.inertiaTensorRotation = planeIntertiaTensorRotation;
One thing to note, as soon as the colliders were enabled the error occurs, so a try / catch block was used. Not exactly an elegant solution but this worked for me. Is there a chance setting this tensor is actually working for you and you just need to deal with the exception?
Your answer
Follow this Question
Related Questions
What does the "compute mesh inertia tensor failed" error mean? 6 Answers
inertiaTensor stopping automatic rotation 1 Answer
How to use custom TensorFlow speech recognition model in Unity3d? 0 Answers
Understanding an error message. 1 Answer
Compute the torque needed to rotate an object at a constant rate 3 Answers