Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I attach a skinned mesh to an animator?

Discussion in 'Scripting' started by Compressed, Jun 27, 2016.

  1. Compressed

    Compressed

    Joined:
    Sep 20, 2014
    Posts:
    59
    Basically I have created and rigged a model in blender (let's call its mesh "mesh1") with couple of animations and then placed the model in a scene. I attached an animator to it and set it up to play the animations. No problems here, animations play and everything is fine.

    Now i added an empty gameobject (let's call it "new1") to the scene and to this object I added a SkinnedMeshRenderer component. Into the mesh field i placed the "mesh1".
    How can I connect connect this new SkinnedMeshRenderer (via script) to that existing animator in the scene (during runtime) so that it plays the animations as well ? (so there would now be two skinnedmeshrenderers playing the same animatons, both controlled by the same animator)
    I do NOT want to have two animators in the scene, i merely need to connect the SkinnedMeshRender placed in new1 to that existing animator in the scene.

    Thanks
     
  2. Simplified

    Simplified

    Joined:
    May 28, 2016
    Posts:
    19
    Kind of hard to understand what you're trying to do, but I'm assuming that you want to replace the animator during runtime. Place your animator controllers in a "Resources" in your "Assets" directory. Use an animator override controller((so that you don't have to create an entirely new animator controller for each object) on your and do the following in a script:

    Code (CSharp):
    1. Animator animate;
    Code (CSharp):
    1.  animate.runtimeAnimatorController =
    2.             Resources.Load("Your controller here")
    3.             as RuntimeAnimatorController;
    You have to set your conditions before or after running that code of course.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    so you want one animator to govern two separate gameobjects? fairly sure they can't do that, so it'll have to be one animator per gameobject...