Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved] Adding Skinned Mesh Rendered Component

Discussion in 'Scripting' started by elelec, Dec 28, 2015.

  1. elelec

    elelec

    Joined:
    Jul 21, 2014
    Posts:
    7
    I've been working on a quick save/load system for a project and almost everything seems to be going smoothly.
    During the loading, I've managed to load most components perfectly. However, there are some issues I've had with the skinned mesh renderer.
    -Note that a model is composed of multiple pieces.
    -Also note that the player model has been put in the scene beforehand, and that's why is looks normal
    This is an example of the issue:



    Here's the script for loading the skinned mesh:
    Code (CSharp):
    1.                 SkinnedMeshRenderer fil = (SkinnedMeshRenderer)compo.com;
    2.                 fil.sharedMesh = theMesh;
    3.                 fil.material = theMaterial;
    4.                 if (gms.ContainsKey(theGameObjectID))
    5.                 {
    6.                     // gm is the gameObject, gms is a dictionary that keeps track of the IDs
    7.                     Helper.SetSkeletonBase(obj, gms[theGameObjectID].gm.transform);
    8.                 }
    9.                 else {
    10.                     Debug.Log("Did not find root bone");
    11.                 }
    And for the skeleton code:
    Code (CSharp):
    1.     public static void SetSkeletonBase(GameObject me, Transform rootBone)
    2.     {
    3.         SkinnedMeshRenderer BonedObject;
    4.         BonedObject = me.GetComponent<SkinnedMeshRenderer>();
    5.         me.transform.localEulerAngles -= rootBone.eulerAngles;
    6.         BonedObject.rootBone = rootBone;
    7.     }
    I've been struggling with this issue for a while and I'm out of ideas .-.

    Thanks in advance ^^
     
  2. kietus

    kietus

    Joined:
    Jun 4, 2013
    Posts:
    54
    Hello,

    I never tried to instantiate a SkinnedMeshRenderer, in my project i load a default character GameObject and i swap mesh inside (with characters using the same rig). But that way seems cool 2.
    Meshes insides your objects seems to have incorrect Transform values. In order to make a test, maybe you can note Transform values for meshes in your gameObject (corpse,head,hear). And set them back when you spawn your object, something like
    Code (csharp):
    1.  
    2.   corpse.transform.localPosition = new Vector3(0.02929915f, 0.2750888f, -0.01536892f);
    3.   corpse.transform.localRotation = new Quaternion(0.3638686f, 0.5793739f, -0.6536294f, 0.3235648f);
    4.  
    (you need to switch editor in debug bug to get full Quaternion values)

    I guess that's a bit uggly, but you can check if your character works. And think to a clever solution after.
     
  3. elelec

    elelec

    Joined:
    Jul 21, 2014
    Posts:
    7
    Nah, this didn't work :/
    I did have the transform values set correctly already, but I also tried resetting them from the SetSkeletonBase function, but still the same issue :/
     
  4. elelec

    elelec

    Joined:
    Jul 21, 2014
    Posts:
    7
    So it seems like I had to add the exact bone transforms to each skinned mesh, so I had to save each single bone into the save file. Anyway, I hope that helps someone in the future