Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Could not resolve 'Left IK Gun_target' because it is not a child Transform in the Animator hierarchy

Discussion in 'Scripting' started by Ahmed_Dyaa, May 17, 2023.

  1. Ahmed_Dyaa

    Ahmed_Dyaa

    Joined:
    Nov 20, 2022
    Posts:
    1
    Hi,
    Currently, I am developing a third person shooter, and the player has two weapon holsters, one for the machine gun and the other for the pistol, and the player switches between them.

    I use "Animation rigging" to set the animation on the weapon.

    - One for the right hand to reach the gun holster and take it out or insert it.
    - Three for the left hand to interact with the gun and hold the gun and reload it.
    - Two for guns holsters to move as part of player body.
    - Three for aim (Head, Body, Hand).

    --------------------------------------------------------------------------------------------------
    But I see several problems in 2 cases.

    1. Take the gun out of its holster.
    2. Switch with another gun in the other holster.

    --------------------------------------------------------------------------------------------------

    - Take the gun out of its holster.

    It works without problems until the gun is inserted and Take it out again.

    This problems appear to me.

    - "Could not resolve 'Skeleton/Hips/Spine/Chest/UpperChest/Right_Shoulder/Right_UpperArm/Right_LowerArm/Right_Hand/Gun Holder/AK-74u(Clone)/Gun Rig/Left IK Gun_target' because it is not a child Transform in the Animator hierarchy." and the same for the rest of the rig.

    - "Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak" appear once.

    - "Internal: deleting an allocation that is older than its permitted lifetime of 4 frames (age = 5)" and the same for the age 15.

    --------------------------------------------------------------------------------------------------

    - Switch with another gun in the other holster.

    Here two cases occur:

    - The first is a switch, and the player is already carrying a gun, so the gun is inserted into it's holster via the script, then the script takes out the required gun.

    The same problems appear in the previous case.

    In addition to

    1- "Could not resolve 'Skeleton/Hips/Spine/Chest/UpperChest/Right_Shoulder/Right_UpperArm/Right_LowerArm/Right_Hand/Gun Holder/AK-74u(Clone)/Mesh/MagMultiParentTarget/Mag MultiParent_Target' because it is not a child Transform in the Animator hierarchy.
    UnityEngine.StackTraceUtility:ExtractStackTrace ()
    ThirdPersonShooter:EquipOrPutAway (Gun) (at Assets/Third Person Shooter/Scripts/Controller/ThirdPersonShooter.cs:513)
    GunButtonUI:OnButtonClick () (at Assets/Third Person Shooter/Scripts/Gun/GunButtonUI.cs:45)
    UnityEngine.EventSystems.EventSystem:Update ()"

    - The second is that the player himself inserts the weapon into his wallet and then takes out the weapon himself immediately or after a while as he wants, like the third case written above.

    The same problems appear in the first and previous case.

    - In addition to : the player performs a strange animation (glitch)
    after inserting the gun into it's holster, and becomes idle , that
    (glitch) is part of the animation of inserting the pervious gun into
    it's holster for one or two frames, and it is not repeated and he
    takes the gun out naturally.

    --------------------------------------------------------------------------------------------------
    **This is the script I used**

    Code (CSharp):
    1. public void EquipOrPutAway(Gun gun)
    2.     {
    3.         if (CanEquipOrPutGunAway())
    4.         {
    5.             if (hasGun())
    6.             {
    7.                 if (gun != activeGun)
    8.                 {
    9.                     WantAnotherGun = true;
    10.                     SelectedGun = gun;
    11.                 }
    12.  
    13.                 isEquippingOrChangingGun = true;
    14.                 IsHolsterIn = true;
    15.  
    16.                 activeGun.audioSource.PlayOneShot(activeGun.HolsterInClip);
    17.                 activeGun.audioSource.minDistance = activeGun.MinSoundDistance;
    18.                 activeGun.audioSource.maxDistance = activeGun.MaxSoundDistance;
    19.  
    20.                 animator.SetTrigger("Holster Gun In");
    21.             }
    22.             else
    23.             {
    24.                 isEquippingOrChangingGun = true;
    25.                 IsHolsterOut = true;
    26.  
    27.                 animator.runtimeAnimatorController = gun.TPSGunController;
    28.  
    29.                 rightIKGun.data.target = gun.holsterTarget.transform;
    30.                 rightIKGunHint.localPosition = gun.RightIKGunHintPosition;
    31.                 rigBuilder.Invoke("Build", 0.01f);
    32.  
    33.                 SelectedGun = gun;
    34.  
    35.                 animator.SetTrigger("Holster Gun Out");
    36.  
    37.             }
    38.         }
    39.     }
    40.  
    41.     public void PutGunAway()
    42.     {
    43.         isEquippingOrChangingGun = true;
    44.         IsHolsterIn = true;
    45.  
    46.         animator.SetTrigger("Holster Gun In");
    47.     }
    48.  
    49.     public void AssignGun()
    50.     {
    51.         activeGun = SelectedGun;
    52.  
    53.         activeGun.audioSource.PlayOneShot(activeGun.HolsterOutClip);
    54.         activeGun.audioSource.minDistance = activeGun.MinSoundDistance;
    55.         activeGun.audioSource.maxDistance = activeGun.MaxSoundDistance;
    56.     }
    57.  
    58.     public void ChangeGunPoint()
    59.     {
    60.         if (IsHolsterOut)
    61.         {
    62.             activeGun.transform.SetParent(GunHolder);
    63.             activeGun.transform.localPosition = activeGun.HandPointPrefab.transform.localPosition;
    64.             activeGun.transform.localRotation = activeGun.HandPointPrefab.transform.localRotation;
    65.         }
    66.         else if (IsHolsterIn)
    67.         {
    68.             activeGun.transform.SetParent(activeGun.holsterPoint.transform);
    69.             activeGun.transform.localPosition = Vector3.zero;
    70.             activeGun.transform.localRotation = Quaternion.identity;
    71.         }
    72.     }
    73.  
    74.     public void SetGunRig()
    75.     {
    76.         leftIKGun.data.target = activeGun.leftIKGunTarget.transform;
    77.         leftIKMagGun.data.target = activeGun.gunIKMagTarget.transform;
    78.         leftIKBoltGun.data.target = activeGun.gunIKBoltTarget.transform;
    79.  
    80.         magMultiParentConstraint.data.constrainedObject = activeGun.Mag.transform;
    81.         magMultiParentTarget.SetParent(activeGun.magMultiParentTarget.transform);
    82.         magMultiParentTarget.localPosition = Vector3.zero;
    83.         magMultiParentTarget.localRotation = Quaternion.identity;
    84.  
    85.         rigBuilder.Invoke("Build", 0.01f);
    86.     }
    87.  
    88.     public void RemoveGun()
    89.     {
    90.         activeGun = null;
    91.  
    92.         rightIKGun.data.target = null;
    93.  
    94.         leftIKGun.data.target = null;
    95.         leftIKMagGun.data.target = null;
    96.         leftIKBoltGun.data.target = null;
    97.  
    98.         magMultiParentConstraint.data.constrainedObject = null;
    99.         magMultiParentTarget.SetParent(magMultiParentConstraint.transform);
    100.         magMultiParentTarget.localPosition = Vector3.zero;
    101.         magMultiParentTarget.localRotation = Quaternion.identity;
    102.     }
    103.  
    104.     public void EndHolsterOut()
    105.     {
    106.         isEquippingOrChangingGun = false;
    107.         IsHolsterOut = false;
    108.     }
    109.  
    110.     public void EndHolsterIn()
    111.     {
    112.         isEquippingOrChangingGun = false;
    113.         IsHolsterIn = false;
    114.  
    115.         if (WantAnotherGun)
    116.         {
    117.             EquipOrPutAway(SelectedGun);
    118.  
    119.             WantAnotherGun = false;
    120.         }
    121.     }
    --------------------------------------------------------------------------------------------------
    The "EquipOrPutAway" function is called by pressing the gun button in the UI or after picking up the gun and instantiate it on it's holster and if player doesn't have any gun then it will be called.

    The "PutGunAway" function is called by specific button in the UI.

    And through the animation "AssignGun" will be called, then "ChangeGunPoint", then "SetGunRig", finally, "EndHolsterOut" is called. this is to take out the gun.

    And through the animation "ChangeGunPoint" will be called, then "RemoveGun", finally, "EndHolsterIn" is called. this is to insert the gun.

    The weight of each rig is controlled by animation in (Reloading, HolsterOut, HolsterIn), and the rest by script

    --------------------------------------------------------------------------------------------------
    Image of the player with rig game objects
     
    Last edited: May 18, 2023