Forum rules - please read before posting.

spawning object from inventory into hand with final ik

Hi. I'm trying to figure out how to get an object from inventory and add it into hand so that Final Ik would hold it. Has anyone else already done this?

Comments

  • If you're just looking to spawn a prefab and parent it to a child of your Player, you may be able to get by with a pair of Object: Add or remove and Object: Set parent Actions.

    Things will be more a little more involved if you need to specifically access a Final IK variable or function. Scripting may be necessary, but the AC side of things should be quite simple.

    Create a prefab of the scene object that represents the Inventory item when held, and assign it as the Item's Linked prefab in its list of properties.

    You can then right-click on the "Linked prefab" field label to get an API reference to it, which you can copy/paste into a custom script.

    For example, to spawn the linked prefab into the scene, you'd use something like:

    GameObject spawnedObject = GameObject.Instantiate (AC.KickStarter.inventoryManager.GetItem (2).linkedPrefab);
    

    Where "2" is replaced by the ID number of the item you wish to spawn.

    How this would then be used by Final IK would be a question for Final IK's developer, but you can run custom code either through Actions (Object: Call event or Object: Send message), or by event hooks.

    Event hooks are the best way to run custom code, as they can be set up to run automatically when certain AC events are triggered - for example, when an item is selected/deselected in the inventory.

    Here's a sample script that uses those two events (OnInventorySelect and OnInventoryDeselect) to spawn and remove an item's prefab from the scene whenever an item is selected/deselected:

    using AC;
    using UnityEngine;
    
    public class InventorySpawnExample : MonoBehaviour
    {
    
        private GameObject spawnedObject;
    
        private void OnEnable ()
        {
            EventManager.OnInventorySelect += OnInventorySelect;
            EventManager.OnInventoryDeselect += OnInventoryDeselect;
        }
    
        private void OnDisable ()
        {
            EventManager.OnInventorySelect -= OnInventorySelect;
            EventManager.OnInventoryDeselect -= OnInventoryDeselect;
        }
    
        private void OnInventorySelect (InvItem invItem)
        {
            OnInventoryDeselect (null);
    
            if (invItem.linkedPrefab)
            {
                spawnedObject = GameObject.Instantiate (invItem.linkedPrefab);
                spawnedObject.transform.SetParent (transform);
            }
        }
    
        private void OnInventoryDeselect (InvItem invItem)
        {
            if (spawnedObject)
            {
                GameObject.Destroy (spawnedObject);
            }
        }
    
    }
    

    The spawned object will also be parented to the object that the script is placed on.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.