0
\$\begingroup\$

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

public void EquipOrPutAway(Gun gun)
{
    if (CanEquipOrPutGunAway())
    {
        if (hasGun())
        {
            if (gun != activeGun)
            {
                WantAnotherGun = true;
                SelectedGun = gun;
            }

            isEquippingOrChangingGun = true;
            IsHolsterIn = true;

            activeGun.audioSource.PlayOneShot(activeGun.HolsterInClip);
            activeGun.audioSource.minDistance = activeGun.MinSoundDistance;
            activeGun.audioSource.maxDistance = activeGun.MaxSoundDistance;

            animator.SetTrigger("Holster Gun In");
        }
        else
        {
            isEquippingOrChangingGun = true;
            IsHolsterOut = true;

            animator.runtimeAnimatorController = gun.TPSGunController;

            rightIKGun.data.target = gun.holsterTarget.transform;
            rightIKGunHint.localPosition = gun.RightIKGunHintPosition;
            rigBuilder.Invoke("Build", 0.01f);

            SelectedGun = gun;

            animator.SetTrigger("Holster Gun Out");

        }
    }
}

public void PutGunAway()
{
    isEquippingOrChangingGun = true;
    IsHolsterIn = true;

    animator.SetTrigger("Holster Gun In");
}

public void AssignGun()
{
    activeGun = SelectedGun;

    activeGun.audioSource.PlayOneShot(activeGun.HolsterOutClip);
    activeGun.audioSource.minDistance = activeGun.MinSoundDistance;
    activeGun.audioSource.maxDistance = activeGun.MaxSoundDistance;
}

public void ChangeGunPoint()
{
    if (IsHolsterOut)
    {
        activeGun.transform.SetParent(GunHolder);
        activeGun.transform.localPosition = activeGun.HandPointPrefab.transform.localPosition;
        activeGun.transform.localRotation = activeGun.HandPointPrefab.transform.localRotation;
    }
    else if (IsHolsterIn)
    {
        activeGun.transform.SetParent(activeGun.holsterPoint.transform);
        activeGun.transform.localPosition = Vector3.zero;
        activeGun.transform.localRotation = Quaternion.identity;
    }
}

public void SetGunRig()
{
    leftIKGun.data.target = activeGun.leftIKGunTarget.transform;
    leftIKMagGun.data.target = activeGun.gunIKMagTarget.transform;
    leftIKBoltGun.data.target = activeGun.gunIKBoltTarget.transform;

    magMultiParentConstraint.data.constrainedObject = activeGun.Mag.transform;
    magMultiParentTarget.SetParent(activeGun.magMultiParentTarget.transform);
    magMultiParentTarget.localPosition = Vector3.zero;
    magMultiParentTarget.localRotation = Quaternion.identity;

    rigBuilder.Invoke("Build", 0.01f);
}

public void RemoveGun()
{
    activeGun = null;

    rightIKGun.data.target = null;

    leftIKGun.data.target = null;
    leftIKMagGun.data.target = null;
    leftIKBoltGun.data.target = null;

    magMultiParentConstraint.data.constrainedObject = null;
    magMultiParentTarget.SetParent(magMultiParentConstraint.transform);
    magMultiParentTarget.localPosition = Vector3.zero;
    magMultiParentTarget.localRotation = Quaternion.identity;
}

public void EndHolsterOut()
{
    isEquippingOrChangingGun = false;
    IsHolsterOut = false;
}

public void EndHolsterIn()
{
    isEquippingOrChangingGun = false;
    IsHolsterIn = false;

    if (WantAnotherGun)
    {
        EquipOrPutAway(SelectedGun);

        WantAnotherGun = false;
    }
}

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


  • When I use "rigBuilder.Build()" it converts all rig weights to zero.

Image of the player with rig game objects

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ Just a guess, but the AK-74u(Clone) Clone part could cause an issue if something is looking it up by Ak-74u only. That is not happening in the posted code but I encountered similar error messages when the name did not match \$\endgroup\$
    – Zibelas
    May 18 at 16:50
  • \$\begingroup\$ No, the script does not search for the gun, I call the "EquipOrPutAway" function and specify the gun that was instantiated before \$\endgroup\$
    – Ahmed Dyaa
    May 19 at 18:28

0

You must log in to answer this question.

Browse other questions tagged .