Forum rules - please read before posting.

Animate new items

Hi
Is there a way to easily play an animation when a new item is added to the inventory? And maybe a sound? So far they just appear, but I find that I need to give the player more feedback.

Comments

  • You can define an "Inventory/Add" event in the Events Editor to run custom logic when the Player recieves an item.

    What logic exactly you run will depend on the animation effect you're looking for. A tutorial on creating a "new item found" popup menu can be found here.

  • Thanks. But I haven't been able to make it work. I'm using Unity UI, and I havent figured out how to get the "0: Inventroy Item" value to an ActionList Object so I can Fade Sprite, or Animate. I followed the tutorial, but I can't seem to access the new inventory item when the actionlist requires an object.

  • Inventory items themselves are just data - if you're looking to affect an object within the scene, you'll need to create a connection between that object and the item. Scripting may be necessary depending on your need.

    What's the specific context - what object in the scene are you trying to animate? The item's graphic within the default Inventory UI?

  • When selecting an item from the inventory, the item grows and attaches to your pointer. When you use it on a hotspot, it shrinks until it disappears.

    However, when adding an element to the inventory using ActionList add to inventory, the sprite simply appears in the inventroy GUI without any animation.

    I want to apply the same growing animation that occurs when selecting an item, to the inventory graphic itself. This would provide simple visual feedback, directing users' attention to the (always-visible) inventory when they receive a new item.

  • When selecting an item from the inventory, the item grows and attaches to your pointer. When you use it on a hotspot, it shrinks until it disappears.

    From use of the "Animated cursor" template, or a custom UI cursor?

    I want to apply the same growing animation that occurs when selecting an item, to the inventory graphic itself.

    Is the Inventory menu on at the time?

    Rely on Unity UI for your Inventory menu, and attach an Animator to each of the slot buttons in the prefab's Hierarchy.

    Assign an Animator controller and define a Trigger parameter named e.g. "AddItem" that causes it to play a growing animation by adjusting the scale of the Image object.

    Test that it works by invoking the Trigger manually in the Animator window at runtime. The next step is to invoke the Trigger automatically through script. I can advise further, but let's get the above set up first.

  • Yep, that worked. I have the animator go from any state to a state called grow item, with a transition trigger "AddItem". If I trigger it manually, all the inventory items grow at the same time.
    (the inventroy is always displayed, and I'm using the CursorUI prefab that came with the demo project)

  • Something like this, attached to each slot object and configured with that slot's index number, should do it:

    using UnityEngine;
    using AC;
    
    public class ItemSlotAnimator : MonoBehaviour
    {
    
        public string menuName = "Inventory";
        public string elementName = "Items";
        public int slot;
    
        void OnEnable () { EventManager.OnInventoryAdd_Alt += OnInventoryAdd; }
        void OnDisable () { EventManager.OnInventoryAdd_Alt -= OnInventoryAdd; }
    
        void OnInventoryAdd (InvCollection invCollection, InvInstance invInstance, int amount)
        {
            var inventoryBox = PlayerMenus.GetElementWithName (menuName, elementName) as MenuInventoryBox;
            if (inventoryBox.GetInstance (slot) == invInstance)
            {
                GetComponent<Animator> ().SetTrigger ("AddItem");
            }
        }
    
    }
    
  • Thanks. That did it.

  • I've found a new issue with the inventory script. The animation plays for all inventory items whenever the inventory is displayed, not just when new items are added; when I hide and reopen the inventory, all items animate again. There's also a problem when adding and removing items in the same Action List—the new item simply replaces the removed one without triggering the new item animation.

    I tried this, but didn't work:

    using UnityEngine;
    using AC;
    using System.Collections.Generic;
    
    public class ItemSlotAnimator : MonoBehaviour
    {
        public string menuName = "Inventory";
        public string elementName = "Items";
        public int slot;
    
        private HashSet<int> trackedItemIDs = new HashSet<int>();
    
        void OnEnable()
        {
            EventManager.OnInventoryAdd_Alt += OnInventoryAdd;
        }
    
        void OnDisable()
        {
            EventManager.OnInventoryAdd_Alt -= OnInventoryAdd;
        }
    
        void OnInventoryAdd(InvCollection invCollection, InvInstance invInstance, int amount)
        {
            // Check if the item ID is already tracked
            if (!trackedItemIDs.Contains(invInstance.ItemID))
            {
                // Add the item ID to the tracked list
                trackedItemIDs.Add(invInstance.ItemID);
    
                // Get the inventory box and check if the slot matches
                var inventoryBox = PlayerMenus.GetElementWithName(menuName, elementName) as MenuInventoryBox;
                if (inventoryBox != null && inventoryBox.GetInstance(slot) == invInstance)
                {
                    // Trigger the animation
                    GetComponent<Animator>().SetTrigger("AddItem");
                }
            }
        }
    }
    
  • The animation plays for all inventory items whenever the inventory is displayed, not just when new items are added; when I hide and reopen the inventory, all items animate again.

    That may be an issue with your Animator - the original script should only invoke the Trigger parameter for the specific item.

    Revert back and place a Debug.Log statement underneath the SetTrigger call:

    Debug.Log ("Trigger: " + gameObject, gameObject);
    

    Does it only show in the Console for the correct item?

  • I changed it like this (to include the else to show when it doesn't enter in the if):

    using UnityEngine;
    using AC;
    
    public class ItemSlotAnimator : MonoBehaviour
    {
    
        public string menuName = "Inventory";
        public string elementName = "Items";
        public int slot;
    
        void OnEnable() { EventManager.OnInventoryAdd_Alt += OnInventoryAdd; }
        void OnDisable() { EventManager.OnInventoryAdd_Alt -= OnInventoryAdd; }
    
        void OnInventoryAdd(InvCollection invCollection, InvInstance invInstance, int amount)
        {
            var inventoryBox = PlayerMenus.GetElementWithName(menuName, elementName) as MenuInventoryBox;
            if (inventoryBox.GetInstance(slot) == invInstance)
            {
                GetComponent<Animator>().SetTrigger("AddItem");
                Debug.Log("Trigger: " + gameObject, gameObject);
            }
            else
            {
                Debug.Log("NOT Trigger: " + gameObject, gameObject);
            }
        }
    
    }
    

    And when addig the third item in the inventroy, the log shows:

    NOT Trigger: btnSlot1 (UnityEngine.GameObject)
    NOT Trigger: btnSlot2 (UnityEngine.GameObject)
    

    but the tird item doesn show anything on the log. (It should show Trigger: btnSlot3 but it doesnt show anything.) And the item just appears in the third slot.

  • Sorry to ask the obvious, but btnSlot3 definitely has the component set up in the same way, and is linked to the Menu Manager? That neither of the logs show up for it in the Console suggests the event itself is not being fired at all.

  • I double checked and btnSlot3 is setup correctly. If have 3 inventory items, and I pick up a fourth the log shows:

    NOT Trigger: btnSlot1 (UnityEngine.GameObject)
    NOT Trigger: btnSlot2 (UnityEngine.GameObject)
    NOT Trigger: btnSlot3 (UnityEngine.GameObject)

    I'm thinking the script is triggerng befrore the inventory items is added instead of after.

  • What's your Menu's "Appear type", and what's your InventoryBox element's "When slot is empty** property set to? It sounds like the opposite - that the script is kicking in too late.

  • Appear type: Manual
    When Slot is Empty: Disable Object

  • edited September 19

    The "Disable Object" option will likely be it - the slot is disabled at the time.

    Does it work if you switch this to "Clear Content"?

  • Yes. That worked. Thanks.

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.