Forum rules - please read before posting.
Tech questions during the weekend? Drop in on the community Discord channel!

Inventory to open and highlight new item added

Hi,

Do you have a tutorial or can help me understand the best way to have an inventory item appear in the centre of the screen then shrink and zoom into the inventory when an item is picked up?

Comments

  • You'll want to animate it as part of a Unity UI-based Inventory, with the item's graphic being a separate RawImage object in the canvas hierarchy.

    When the player takes an item, you can use scripting to update the RawImage's texture with that of the item:

    public UnityEngine.UI.RawImage rawImage;
    
    private void OnEnable () { AC.EventManager.OnInventoryAdd += OnInventoryAdd; }
    private void OnDisable () { AC.EventManager.OnInventoryAdd -= OnInventoryAdd; }
    
    private void OnInventoryAdd (AC.InvItem invItem, int amount)
    {
        rawImage.texture = invItem.tex;
    }
    

    It's a lot easier if you have the item zoom to the same position each time, i.e. a "bag" icon rather than the actual position of the item in the inventory. This way, you can animate the RawImage's position through a regular Animator component.

  • ok thanks! So what do i attach this script to?

  • A RawImage component that's enabled at the time of the inventory addition.

  • i think i may have got this script slightly wrong? Getting errors:

    (Must derive from monobehaviour)

    using UnityEngine;

    public class InventoryZoom
    {
    public UnityEngine.UI.RawImage rawImage;

    private void OnEnable() { AC.EventManager.OnInventoryAdd += OnInventoryAdd; }
    private void OnDisable() { AC.EventManager.OnInventoryAdd -= OnInventoryAdd; }
    
    private void OnInventoryAdd(AC.InvItem invItem, int amount)
    {
        rawImage.texture = invItem.tex;
        //trigger the animation here
    }
    

    }

  • How would i incorporate code to set off my animation and maybe a sound too?
  • Use:

    public class InventoryZoom : MonoBehaviour
    

    How would i incorporate code to set off my animation and maybe a sound too?

    You can use another script, or the Object: Animate / Sound: Play Actions.

  • ok, so i have attached it to the RawImage within my inventory, but nothing is happening when I pick up my item? I don't think my object animate is linking in scene as the Inventory is a prefab? Am i doing this wrong?

    https://www.dropbox.com/sh/5g5lgw6euc4uq4x/AADY16ZhEA9d4rO40MrQb7EUa?dl=0

  • You'll want to run the Action from an ActionList asset, since that way you can use parameters to run the same list each time you take an item - see this tutorial.

    If you assign the Animator from the prefab into an asset, it should record a Constant ID that can be used to find it in the scene.

    Alternatively, the script can also be updated to trigger the animation directly:

    GetComponent<Animator>().SetTrigger ("MyTriggerParameter");
    
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.