Forum rules - please read before posting.

I want to click on a hotspot move 2dsprite to center of screen, scale it to fit 60% of screen....

I remember downloading some sort of script for this tutorial where one could click on the frame photo, the object will move/scale to center of screen, it can be then rotated, then either placed back or taken to inventory.

Sadly I can't seem to find it :(

What I want to do is similar but for 2D workflow. No rotation, I want to click on a hotspot/2dsprite, then move the object to center of screen, scale it to fit 60% of screen (regardless of sprite scale in scene/or the asset) then add it to the inventory.

I've already managed to click add to inventory. Works fine. I move scene 2D object outside the camera view using the Actions node. All good.

Now I want to make this more visual appealing by first move and scale to screen center then teleport the object out of the screen view and add the inventory item.

I'm stuck, what I've tried.

Using Object:Transform Scale:To option, the scale half works because I can't seem to control the value the object will scale.

Using Object Transform and Translate:To doesn't work because I don't know the screen center. I've tried connecting a global variable to a custom script to calculate the screen to world position but that doesn't work because I don't have hitboxes for the the Physics.Raycast for Camera.main.ScreenPointToRay(new Vector2(Screen.height / 2, Screen.width / 2));

I also don't have physics and I don't want to add colliders everywhere. I just want a simple animation to scale translate to center of screen.

The way the unity scene is set up is that I have multiple cameras and I switch between them. So I can't have empty transforms everywhere to hack/get the position the item should move to.

Comments

  • Sadly I can't seem to find it

    You may be referring to this script on the AC wiki:
    https://adventure-creator.fandom.com/wiki/Return_PickUps_to_original_position

    Using Object Transform and Translate:To doesn't work because I don't know the screen center.

    You can do this with a Marker that's parented to the MainCamera, with a local position of (0,0,1) - i.e. just in front of it.

    The Object: Transform Action's Copy Marker method can then be used to position the sprite dynamically to the Marker's current position.

    The scale will be need to be adjusted on a per-sprite basis if each sprite has a different size. If the above works, share screenshots of a couple of examples and we can look into tackling that - likely with a custom script.

  • Ok, it works, Thank You. Adding only one Marker to the MainCamera works with all the rest of the cameras :D

    For the scale I think I could use the link variable and have a script that calculate the required scale value based on sprite size and the screen percentage or some default bounding box. It should be some math thing, I will search the web

    There is any trick to make these steps automatic and not need to duplicate the entire action nodes for each object in the unity scene?

  • You wouldn't need an AC variable - as you can calculate the scale, and apply it to the Marker, just before moving the object.

    To call a custom function from an ActionList, make the function public, attach to an object in the scene, and use the Object: Send message Action's "Custom" option.

    There is any trick to make these steps automatic and not need to duplicate the entire action nodes for each object in the unity scene?

    Yes - you can make use of ActionList parameters, which allow you to "recycle" ActionLists by modifying certain Action fields at runtime. A tutorial on their usage can be found here.

  • Everything worked perfectly, the ActionList with parameters are great.

    I didn't knew that copy marker will copy the scale also, which means there is no need for two nodes, one for scale and one for translate.

    Also, as long as I keep track of the sprite asset scale there is no need to calculate anything anymore.

    If the sprite assets at scale 1 covers the area of the screen when it will be "zoom in" then I can keep the Screen Center Marker to scale 1. The sprite placed in the scene will be smaller (scaled to a lower value), to account for perspective and distance, then it will always scale up to the correct size.

    Thank You for your help, this improved my workflow tremendously.

    I have an optional question but is not super important is more of a style thing.

    So right now the object will be clicked in scene, translate and scale to center of screen, then translate and scale to a second marker that is positioned where the "inventory icon" is located. This second marker is scaled down so the object will nicely scale down also. Then it fades away to represent that it was added to the inventory. All works perfectly.

    But what would be nice would be to

    • show inventory bar,
    • somehow figure out where the next empty inventory spot is located
    • translate, scale, fade towards that position.
    • apply some scale fade to the inventory item icon when adding it to the inventory

    if for example all the visible inventory spots are filled then a scroll to reveal a new empty spot may be required.

    All this sounds a bit complicated so if there is no easy solution fine with me :)

  • It's possible, but is a bit tricky.

    To show the Inventory bar, you'd use the Menu: Change state Action to turn on your Inventory menu - but this'll only work if its Appear type is set to Manual, and not its default Mouse Over value. Alternatively, you could create a duplicate of the Menu purely for this effect and show that instead.

    To move the new item to the Inventory, you can repurpose your existing "Inventory icon" Marker, and reposition it to the correct spot.

    You can get the screen-space position of a Menu Element slot with the GetSlotCentre function.

    For this function, you'd need to pass in the slot index you'd want to get the position for - in your case, the first-empty slot in the InventoryBox. Something along these lines should do it:

    Menu inventoryMenu = PlayerMenus.GetMenuWithName ("Inventory");
    MenuInventoryBox inventoryBox = inventoryMenu.GetElementWithName ("InventoryBox") as MenuInventoryBox;
    
    int firstEmptySlot = 0;
    for (int i = 0; i < inventoryBox.GetNumSlots (); i++)
    {
        if (inventoryBox.GetItem (i) == null)
        {
            firstEmptySlot = i;
            break;
        }
    }
    
    Vector2 screenPosition = inventoryMenu.GetSlotCentre (inventoryBox, firstEmptySlot);
    

    To have the icon fade out as it moves, attach the Sprite Fader component and use the Object: Fade sprite Action.

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.