[BUG] SetItemIKTarget problem

WeiYiHua

Member
1. Create a new project with Unity 2021.3.13f1.
2. Download and install Third Person Controller 3.0.6.
3. Set the IK Target of the weapon.
1676884554961.png
4. For the convenience of testing, modify the code.
UltimateCharacterController\Scripts\Objects\CharacterAssist\ItemPickup.cs

Code:
var itemSetManager = inventory.gameObject.GetCachedComponent<ItemSetManagerBase>();
itemSetManager.UpdateItemSets();

//if (string.IsNullOrWhiteSpace(m_ItemSetName) == false) {
//    if (itemSetManager.TryEquipItemSet(m_ItemSetName, m_ItemSetGroup, forceEquip, false)) {
//        return true;
//    } else {
//        Debug.LogWarning($"Cannot equip item set '{m_ItemSetName}' it might be invalid, disabled or might not exist.");
//    }
//}

for (int i = 0; i < m_ItemDefinitionAmounts.Length; i++) {
    var itemIdentifierAmount = m_ItemDefinitionAmounts[i];
    for (int j = inventory.SlotCount - 1; j >= 0; j--) {
        var characterItem = inventory.GetCharacterItem(itemIdentifierAmount.ItemIdentifier, j);
        if (characterItem == null) { continue; }

        // Only equip if the item is not already equipped or about to be equipped.
        // It can sometimes be equipping from the Pickup event.
        if (itemSetManager.IsItemContainedInActiveItemSet(m_ItemSetGroup, characterItem.ItemIdentifier) ||
                        itemSetManager.IsItemContainedInNextItemSet(m_ItemSetGroup, characterItem.ItemIdentifier)) {
            continue;
        }

        var itemSet = itemSetManager.GetItemSet(characterItem.ItemIdentifier, m_ItemSetGroup, true);
        if (itemSet == null) { continue; }

        //itemSetManager.TryEquipItemSet(itemSet, forceEquip, false);
    }
}

5. Equip the weapon with IK target.
6. Equip another weapon that doesn't exist in your inventory.

GIF 2023-2-20 17-27-30.gif

The soft equipped CharacterItem overrides the IK Target of the equipped CharacterItem.

Please fix it and where relevant, thanks!
 
I'm not completely following what you are doing - why aren't you using TryEquipItemSet? This is how ItemSets are equipped which in turn correct equips the item and sets the IK.
 
The problem may be here, the SetVisibleObjectActive method will be called in the Start method of CharacterItem, and this method will refresh the Target IK of CharacterIKBase in ThirdPersonPerspectiveItem.

UltimateCharacterController\Scripts\Items\CharacterItem.cs
Line: 396

Code:
/// <summary>
/// Adds the item to the inventory and initializes the non-local network player.
/// </summary>
private void Start()
{
    ...
    SetVisibleObjectActive(false, m_Inventory.GetItemIdentifierAmount(m_ItemIdentifier) > 0);
    ...
}
 
Ah, I see. Thank you, I have this fixed for the next version. It required changing a few files but I am now using the three parameter version of ThirdPersonPerspectiveItem.SetActive for the Start method so it no longer tries to set the IK targets within Start.
 
I found that there are similar problem to call SetVisibleObjectActive methods in the Remove method of CharacterItem.

UltimateCharacterController\Scripts\Items\CharacterItem.cs
Line: 1250

Code:
/// <summary>
/// Removes the item from the character.
/// </summary>
public void Remove()
{
T_PERSON_CONTROLLER
    if (m_FirstPersonPerspectiveItem != null) {
        m_FirstPersonPerspectiveItem.Remove();
    }

    if (m_ThirdPersonPerspectiveItem != null) {
        m_ThirdPersonPerspectiveItem.Remove();
    }
    if (m_ItemActions != null) {
        for (int i = 0; i < m_ItemActions.Length; ++i) {
            m_ItemActions[i].Remove();
        }
    }
    SetVisibleObjectActive(false, false);
}

Example:

1. Create a new project with Unity 2021.3.19f1.
2. Download and install Third Person Controller 3.0.6.
3. Set the IK Target of the weapon.
1678010194728.png
4. Pick up Frag Grenade.
5. Use up the FragGrenadeWeaponLeft.

1.gif

6. The weapon's IK is cleared when the grenade runs out.
 
Last edited:
Top