Forum rules - please read before posting.

Animate inventory

Hi, Is it possible to animate an inventory object being dragged on to another? Then for the combine action script to run? I almost want the narrator to take control of your inventory to show you how to combine items, so you would start the scene, player would speak, then the narrator would speak, the inventory would open and the objects would be independently moved on top of eachother as combining? Then once combined and my combine script run, the player takes back control

«1

Comments

  • You would have to create the sequence as an animation clip - with the dragged object being part of your UI, hidden normally.

  • how would i determine where the object is in my inventory? As depends on when the objects are picked up as to their positioning in the inventory. any ideas?

  • You can use a setup similar to the one mentioned in the last answer of this thread to position a UI RectTransform over the mouse position.

    Essentially you'd have your animation just control the dragged object's visibility, and separately use a script to move it to its intended position at the same time.

    Something like this, by calling the script's MoveFromCursor function:

    using UnityEngine;
    using System.Collections;
    
    public class AnimateCursor : MonoBehaviour
    {
    
        public RectTransform cursorRectTransform;
        public Canvas canvas;
        public float moveDuration;
        private Vector2 originalPosition;
    
        private void Start ()
        {
            originalPosition = cursorRectTransform.anchoredPosition;
        }
    
    
        public void MoveFromCursor ()
        {
            StopAllCoroutines ();
            StartCoroutine (MoveFromCursorCo ());
        }
    
    
        private IEnumerator MoveFromCursorCo ()
        {
            Vector2 mousePositionUI = Input.mousePosition / canvas.transform.localScale.x;
            float timer = moveDuration;
            while (timer > 0f)
            {
                float p = 1f - (timer / moveDuration);
                cursorRectTransform.anchoredPosition = Vector2.Lerp (mousePositionUI, originalPosition, p);
    
                timer -= Time.deltaTime;
            }
        }
    
    }
    

    This isn't involving AC, however.

  • This is awesome, but I am wanting the inventory objects that would already be in my inventory to animate on top of eachother independent of player, as if being taken control, once entering a room, like this screenshot:

    https://www.dropbox.com/s/z6ps4r99czn6i7n/animateinvobjects.png?dl=0

  • A given item's RectTransform can be found by getting it's UISlot from the InventoryBox element:

    using UnityEngine;
    using System.Collections;
    using AC;
    
    public class AnimateCursor : MonoBehaviour
    {
    
        public Vector2 offset;
        public RectTransform cursorRectTransform;
        public int itemToMoveFrom;
        public int itemToMoveTo;
        public Canvas canvas;
        public float moveDuration = 3f;
    
        public string menuName = "Inventory";
        public string inventoryElementName = "InventoryBox";
    
    
        public void AnimateCombine ()
        {
            StopAllCoroutines ();
            StartCoroutine (AnimateCombineCo ());
        }
    
    
        private IEnumerator AnimateCombineCo ()
        {
            MenuInventoryBox inventoryBox = PlayerMenus.GetElementWithName (menuName, inventoryElementName) as MenuInventoryBox;
    
            int fromSlot = inventoryBox.GetItemSlot (itemToMoveFrom);
            RectTransform fromRectTransform = inventoryBox.uiSlots[fromSlot].GetRectTransform ();
    
            int toSlot = inventoryBox.GetItemSlot (itemToMoveTo);
            RectTransform toRectTransform = inventoryBox.uiSlots[toSlot].GetRectTransform ();
    
            Vector2 fromPosition = fromRectTransform.position / canvas.transform.localScale.x;
            Vector2 toPosition = toRectTransform.position / canvas.transform.localScale.x;
    
            Vector2 parentOffset = canvas.GetComponent<RectTransform>().sizeDelta / 2f;
            fromPosition -= parentOffset;
            toPosition -= parentOffset;
    
            fromPosition += offset;
            toPosition += offset;
    
            float timer = moveDuration;
            while (timer > 0f)
            {
                float p = 1f - (timer / moveDuration);
                cursorRectTransform.anchoredPosition = Vector2.Lerp (fromPosition, toPosition, p);
    
                timer -= Time.deltaTime;
                yield return null;
            }
        }
    
    
        private Rect RectTransformToScreenSpace (RectTransform transform)
        {
            Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
            return new Rect((Vector2)transform.position - (size * 0.5f), size);
        }
    
    }
    
  • Ok, and forgive my newbieness but how/where do I use this script ? Thanks Chris
  • Attach it to your Inventory menu's prefab so that you can assign the cursor image in its Cursor Rect Transform Inspector field.

    To trigger the effect, use the Object: Send message Action to trigger its AnimateCombine function.

  • So, I am still very confused here, I do not want the cursor image to change, I just want to, independently of the player doing it, move an inventory item onto the top of another inventory item, ie the matches onto the unlit candle. The mouse cursor doesn't have to change to anything but the wait icon. As if the game itself is showing the player how to combine objects.

  • That's as I understood it.

    Don't confuse the "Cursor Rect Transform" with the actual cursor. The script doesn't affect the cursor, but a RectTransform in the Canvas hierarchy that represents the item/cursor from the inventory.

  • So, have I set this up correctly? Currently the send message command is not doing anything (Do I need to set up an animation, or cal the inventory to open first?)

    See screenshots:

    https://www.dropbox.com/sh/c6be149b0qg1uaq/AACjDX36z7llOcaSfbF6w80la?dl=0

    • The Cursor Rect Transform Inspector field needs to be set to the object you wish to move - not the RectTransform boundary
    • The prefab root will need to have a Constant ID component with an ID number that matches that listed in the Action
    • The Inventory menu will need to be open at the time the Action is called
  • So just to clarify, I will need to make sure that the inventory item is in slot button 1 for example, and I would put btnSlot1 into the Cursor Rect Transfotm?

    What is Item To Move From and to then? Sorry I am not sure what is to go in here.

  • Cursor Rect Transform is reserved for the Image that represents the item being moved from the original item slot to the item being combined with. This is the dynamic aspect, and the part that needs scripting, because you can't predict in which slot these items are.

    The "Item To Move From/To" fields are how the script works out where this Image should start from and end up. These fields should be set to the IDs of the inventory items involved. The script will look up which slots they appear in inside the Menu's InventoryBox element - whose names will also need to be supplied in the Inspector.

  • edited December 2021

    So, I put nothing in the Cursor Rect Transform slot?

    When you say IDs of the inventory items, do you mean the Label or the Name?

    And do i need to edit the offset?

    Thanks

  • So, I put nothing in the Cursor Rect Transform slot?

    You put in the RectTransform that should be moved as part of the animation. This should be an Image that will represent the item being moved from one slot to the other.

    When you say IDs of the inventory items, do you mean the Label or the Name?

    An item's ID is its number beside its name in the Inventory Manager.

    And do i need to edit the offset?

    Not necessarily - once you have it working, you can apply one if you wish.

  • do i need to create an animation for the RectTransform image?

  • Not to move it - that's what the script does.

  • edited December 2021

    Would this Raw Image be invisible otherwise? Or would I need to have it turned off before actioning the script? See screenshot of where I have placed it, plus other photos of all the assets and scripts, as i still doesn't do anything with the settings I currently have:

    https://www.dropbox.com/sh/d2ia3y2d7wsyo7s/AAAzTErAzBiLX4mKMTQ4ahJ6a?dl=0

  • The script does not control the Image's sprite, and does not affect its position outside of the function call. You will need to set its appearance to match the item you want to simulate being dragged.

    Don't worry about the item being invisible beforehand for now. Keep the object selected in the Hierarchy and viewed in the Scene window at runtime to check if its position is being moved. If it's a child object of a layout group (e.g. a Horizontal Layout Group on the Panel), then that will create an unwanted conflict.

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.