Search Unity

Question How can I grab a weapon from behind on the player back ?

Discussion in 'Animation' started by shamenraze1988, Mar 31, 2021.

  1. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    I downloaded mixamo animation and the animation is working fine.
    On the player back as a child there is a capsule.

    I want that when the animation at second 1 when the player hand is on the back to make the capsule child of the player hand.

    Two things I need to do :

    1. Make the capsule child of the player hand at some specific second in the animation.

    2. Then when the animation continue the capsule should move logic with the animation.



    This screenshot show the moment the animation make the player hand to be on the back this is the second the weapon(capsule) should be child of the player hand I think.

    Not sure how to do all that stuff.
     
  2. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    For the child problem I used a Coroutine :

    Code (csharp):
    1.  
    2. void Start()
    3.     {
    4.         playerAnimator = player.GetComponent<Animator>();
    5.  
    6.         if (turnOnOffPlayerAnimator)
    7.             playerAnimator.enabled = false;
    8.  
    9.         StartCoroutine(GrabWeapon());
    10.     }
    11.  
    Then

    Code (csharp):
    1.  
    2. IEnumerator GrabWeapon()
    3.     {
    4.         yield return new WaitForSeconds(1f);
    5.  
    6.         var hand = GameObject.Find("rig_hand.R");
    7.  
    8.         capsuleToBeChild.parent = hand.transform;
    9.     }
    10.  
    The problem now is the rotation and/or the position of the capsule when it's child. The result now is :
    The whole rotation and/or position is wrong :

     
  3. bobadi

    bobadi

    Joined:
    Jan 3, 2019
    Posts:
    675
    1. can use animation event [add a function to parent the weapon to hand]
    2. add a child object to the hand w position and rotation how the weapon should look in hand [and parent the weapon to that object when animation event is called]
     
    shamenraze1988 likes this.
  4. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208

    Make it child I used Coroutine :

    At the Start :

    Code (csharp):
    1.  
    2. hand = GameObject.Find("rig_hand.R");
    3. StartCoroutine(GrabWeapon());
    4.  
    Then

    Code (csharp):
    1.  
    2. IEnumerator GrabWeapon()
    3.     {
    4.         yield return new WaitForSeconds(1f);
    5.  
    6.         capsuleToBeChild.parent = hand.transform;
    7.     }
    8.  
    Then I tried a lot with the rotation and the position of the capsuleToBeChild after it's child. tried any rotation/s and position/s I can think of but it's never fit the animation.

    It does become child smooth and nice and the next 1 second it also move with the hand fine but then it's getting all wrong with the animation and the weapon. The capsuleToBeChild is the weapon in this case.
     
  5. bobadi

    bobadi

    Joined:
    Jan 3, 2019
    Posts:
    675
    add a child object to the hand [weapon holder]. add the weapon as the child object of the weapon holder. press play. position and rotate the weapon holder until it's good. copy the transform values [position/rotation]. press stop.

    paste the transform values to the weapon holder.

    +anytime you grab the weapon make sure it has the same position and rotation values as the weapon holder
    weapon.position = weaponholder.position;
    weapon.rotation = weaponholder.rotation;
    weapon.parent = weaponholder;