Search Unity

How do I give my character a weapon?

Discussion in 'Animation' started by cdutoit, Feb 3, 2014.

  1. cdutoit

    cdutoit

    Joined:
    Jul 20, 2013
    Posts:
    34
    Hi - I know the title is strange and sorry for the newbie question but I've been struggling with this for a few days and not making any progress.

    I am trying to take a model from the asset store (a soldier for example) and have him hold a gun while he walks around so he is ready to shoot. There are many good assets in the store for a soldier, and many good assets for a gun. The question is, how do I combine them and have the character walk holding the gun?

    Because of the AI toolkit I'm using, I have to use mecanim (not legacy). The problem is that the animations I have found show a character running and swinging his arms, where I need a character running and holding a weapon. I'm not trying to be fancy with raising or lowering a weapon...I just want a simple character running around with his weapon pointed at all times.

    I don't have Pro so I can't use IK's. I'm sure I'm missing something and would be very grateful for any and all advice and pointers here, even suggested reading/articles.

    Thanks
    Chris
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,698
    You need a gun-holding animation, such as this one on Mixamo or this Soldier Animation Pack. Or get Cinema Mo-Cap and record your own.

    Attach the gun model as a child of the character's hand bone. You can add a child object to the hand called "attach point" that's positioned and rotated exactly where you want it for the gun. (Using the same technique, you can add attach points for helmets, equipment on belts, etc.)

    To run-n-gun, play the gun-holding animation on a layer that only affects the upper body. Then the lower body is free to run, jump, whatever.
     
  3. cdutoit

    cdutoit

    Joined:
    Jul 20, 2013
    Posts:
    34
    TonyLi - thanks for taking the time to respond. Thank you too for the information about playing a gun-holding animation one one layer, leaving the lower body free to run. I have never even known about this idea...and still don't :) but have somewhere to go read up on this. I think that's the part that was confusing me about how I can take a gun holding animation and then combine it with a running animation. I would never have guessed about this layers concept so I will go do some reading now.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,698
  5. cdutoit

    cdutoit

    Joined:
    Jul 20, 2013
    Posts:
    34
    Well - I managed to get this working more or less!! Thanks so much for the tip. I say more or less in that it doesn't look 100% yet and I'm not even sure how I got it all working but with a lot of trial and error on the mask and stuff, seems to be almost there! Phew. Progress!

    Thanks again!
     
  6. Ajesh_Yohan

    Ajesh_Yohan

    Joined:
    Jun 11, 2016
    Posts:
    5
    Sir I have animated a character using mixamo, i need to attach a two handed weapon, if i add weapon to one hand it moves perfectly but the other hand move through the weapon.
     
  7. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,698
    Hi - You need to use Inverse Kinematics (IK) to attach the other hand to the weapon. It requires scripting or using an asset from the Asset Store. I don't remember which assets will do this for you.
     
  8. Ajesh_Yohan

    Ajesh_Yohan

    Joined:
    Jun 11, 2016
    Posts:
    5

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class ikhandlernew : MonoBehaviour {


    Animator anim;

    public float lhweight = 1;
    public Transform lhtarget;

    //public float rhweight = 1;
    //public Transform rhtarget;

    public Transform weapon;
    public Vector3 lookpos;

    void Start ()
    {
    anim = GetComponent<Animator> ();
    }

    // Update is called once per frame
    void Update ()
    {

    }

    void OnAnimatorIk()
    {
    anim.SetIKPositionWeight (AvatarIKGoal.LeftHand, lhweight);
    anim.SetIKPosition (AvatarIKGoal.LeftHand, lhtarget.position);
    anim.SetIKRotationWeight (AvatarIKGoal.LeftHand, lhweight);
    anim.SetIKRotation(AvatarIKGoal.LeftHand, lhtarget.rotation);

    //anim.SetIKPositionWeight (AvatarIKGoal.RightHand, rhweight);
    //anim.SetIKPosition (AvatarIKGoal.RightHand, rhtarget.position);
    //anim.SetIKRotationWeight (AvatarIKGoal.RightHand, rhweight);
    //anim.SetIKRotation(AvatarIKGoal.RightHand, rhtarget.rotation);


    }
    }



    i have added weapon as the child to right hand and it weapon moves perfectly but glitch occurs in left hand is there any problem with the above script
    I have added an empty game object in weapon for lefthand target
     
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,698
    Sorry, that's not enough information to say. Are there any errors or warnings in the Console window? Are you sure that lweight is assigned? Are you trying to move the left hand too far?
     
  10. Ajesh_Yohan

    Ajesh_Yohan

    Joined:
    Jun 11, 2016
    Posts:
    5
    there's no error or warning, i have assigned lhtarget to an empty game object in weapon, lhweight value is float 1 ,
    just trying to fix the left hand with weapon, while running left hand goes through the weapon
    do you have any sample source code to learn about ik to attach two handed weapon
    i googled a lot but doesn't work
     
  11. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,698
    Check YouTube for inverse kinematics tutorials in Unity. Everyone has their own learning style, so some tutorials may work better for you than others.
     
    Ajesh_Yohan likes this.
  12. Ajesh_Yohan

    Ajesh_Yohan

    Joined:
    Jun 11, 2016
    Posts:
    5
    Sir i found out the problem, i have not changed the character rig to humanoid. Thanks for your help.
    I have another doubt,
    i am using twin virtual joystick for android and the game is third person isometric view, i am using the first stick to move in 8 direction, the other one is to turn and shoot the character like minimilitia but not 2d, the problem is with setting up left and right turn animation need help with this
     
  13. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,698
    I recommend setting up a big blend tree that blends on different parameter values. You might want to examine the free Locomotion Starter Kit on the Asset Store to get some ideas.