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

Mecanim avatar how to have one animation with an extra mesh like a gun ?

Discussion in 'Animation' started by wonkza, Feb 29, 2016.

  1. wonkza

    wonkza

    Joined:
    Mar 9, 2015
    Posts:
    29
    I know this is a basic question, I made a character with the rig and a couple of animation with maya for my game that work with mecanim ( idle, walk, interact ecc...). It all works fine so far inside Unity, the avatar switches between the various animations.

    My question is how do I add an object on one of those animations like my character pulling out a gun ? Since there is no gun on the character avatar, I want it to look like it comes out of his pocket. The hand goes into the pocket and he magically pulls out the gun.
     
    Last edited: Feb 29, 2016
  2. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Hi wonkza.

    There are several steps needed to make this happen, if I'm thinking correctly about what you want, but don't worry its not as bad as I might make it seem lol. But I'll try to go into some depth here.

    Step 1: Parent your gun to your characters hand

    If you're not "optimizing" your gameobject in your characters import "Rig" settings (the "Rig" tab), then all you have to do is make whatever gun model you want to be a child of your character's hand bone transform (or whatever transform you want the gun to follow -- usually it's a hand or forearm bone etc....). To do this, you can drag your gun gameobject in your scene hierarchy onto the bone transform underneath your character gameobject. If you character has "Optimize GameObject" checked in its rig import settings though, then you have to make sure you expose that specific bone transform in the rig settings. But make sure your gun is a child of the bone you want it to follow.


    Step 2: Turn off your gun mesh renderer (or gameobject)

    Once you've successfuly parented your gun gameobject to your characters hand, then you'll have to do a little bit of coding for this effect to work (at least that's how I do it). What you do is, turn OFF the gun gameobject, or the mesh renderer of your gun -- so that it is invisible to the player until the character plays a specific animation. At a certain time in the animation you want the gun to appear on, you have an "event" in that Animation fire off a function on a script that will turn back on your gun gameobject (or gun's mesh renderer) at the specific time you need in the animation. And so it will look like your character is pulling out the gun at that time. Essentially, your gun is always where you need it the entire time your character is in the game (it will follow the location of your characters hand at all times), but the player will only see it when you want them to see it.


    Step 3: Add an event to your characters specific animation

    So, you'll have to choose which animation you want to work with (usually a "I'm pulling out my gun" animation you'll have to get from somewhere if it's not already with your character). You go into your character's Animation tab on its import settings (the original model file you imported). You find the characters "I'm pulling out my gun" animation. You go down to "Events" (I think it is) on that specific animation. Move the timeline to the specific time in the animation you want your gun gameobject or mesh renderer to appear. Add an event marker at that time. Click on the event marker. Add the "function/method name" you want the event to play at that time into the "event marker". Apply the settings to your character.


    Step 4: Code your function/method to turn on your gun when the animation event gets played

    Now, you'll have to create a script (c# or Unityscript, it doesn't matter). You'll be attaching this script to your root transform on your character, that is your main character gameobject. In code in the Start function, reference the gun gameobject in your characters hierarchy (usually the path is something like MyCharacter --> MyCharactersRigMainBone --> TheSpine --> TheRightShoulder --> TheRightForearm --> TheRightHand). It's usually a really long reference that can be kind of annoying lol. Then create a function named exactly as it was named in your animation event. In code, have that function turn ON your gun's mesh renderer/gameobject.

    I'll provide some sudo code for you here, but you'll have to do this yourself if you don't know how already (I can try to help you though). So, something like:

    Code (CSharp):
    1. private GameObject myGunGameObjectReference;
    2.  
    3.  
    4. Start {
    5.      myGunGameObjectReference = transform.Find("BeginningBoneOfRig/NextBoneInRig/AnotherBoneInRig/AnotherBone/FinallyTheHandBone").gameObject;
    6. }
    7.  
    8.  
    9. Update {
    10.    //You don't need anything in the update function, just an extra function on this script named exactly the same as on your animation event marker
    11. }
    12.  
    13.  
    14.  
    15. //FUNCTIONS
    16.  
    17. void MyGunAnimationEventFunction () {
    18.      //Turn on my gun gameobject
    19.      myGunGameObjectReference.SetActive(true);
    20. }
    21.  
    22.  
    23. //You can also make another function that can be called by another animation to turn off your gun gameobject when your characer puts the gun back in its pants (or holster)
    24. void TurnOffMyGunGameObject() {
    25.      myGunGameObjectReference.SetActive(false);
    26. }
    27.  
    28.  
    So, if you were to use my script with those exact functions. Then in your "I'm pulling out my gun" animation on your character, the event marker would need the "MyGunAnimationEventFunction" -- exactly as it is named there. Hope that might help some.


    **Edit: Sorry just though of this, but my script may not work exactly, because if you try to reference a GameObject at the Start of the game that is disabled, it may not reference the GameObject correctly. So you might want to leave your Gun GameObject ON, but simply turn on/off it's mesh renderer. This is essentially the same exact code, but you have to put "GetComponent<MeshRenderer>()" to access the guns mesh renderer in code. I can help you further if you need more help.
     
    Last edited: Feb 29, 2016
    theANMATOR2b likes this.
  3. wonkza

    wonkza

    Joined:
    Mar 9, 2015
    Posts:
    29
    Thanks for the answer, I will try it asap