Forum rules - please read before posting.

Closing a popup Sprite Graphic by clicking anywhere on screen

edited May 2020 in Technical Q&A

Hi,

i use a hotspot2D and sprite with Moveable component and Object > Transfer to pop up a sprite graphic puzzle on screen with buttons / hotspots to switch on/off various lights etc.
Everything works 100%.

I want the player to be able to close this sprite graphic by clicking anywhere on the screen except the sprite graphic area.
I don't want to use a button hotspot to close it.
Similar to how Machinarium opens and closes all the puzzle graphics on screen. As simple as that.

I've tried a few things with no success.
On my mind it would be ideal to have a trigger covering my sprite graphic and then somehow tell it: if player clicks outside the trigger then activate actionlist A and so hide the sprite graphic.
Similar to Trigger type "On enter / On exit" but with left mouse click.

Is something like that possible? and if not what is the best way?

Thank you

Comments

  • edited May 2020

    If you don't want to rely on a menu or Hotspot covering the screen, it's best done with a simple script. If the sprite itself is a Hotspot, you can check to see if it's active when the mouse is clicked, and then run an ActionList:

    using UnityEngine;
    using AC;
    
    public class ClickOffHotspot : MonoBehaviour
    {
    
        public ActionList actionListToRun;
        public Hotspot hotspotToNotBeOver;
    
        private void Update ()
        {
            if (Input.GetMouseButtonDown (0) && KickStarter.playerInteraction.GetActiveHotspot () != hotspotToNotBeOver)
            {
                actionListToRun.Interact ();
            }
        }
    }
    
  • Hi,

    1)
    i just add on my sprite a Hotspot Component and a new script.
    I copy paste the code and name the script ClickOffHotspot.

    The script shows two boxes, the action list to run (which i set) and the Hotspot to not be over, which is the same sprite that i added the hotspot before.

    What am i doing wrong?
    https://imgur.com/HBaIwoZ

    The thing is that even when i add the script to a random object then when i play none of the hotspot in the game work.

    2)
    You mentioned "If you don't want to rely on a menu or Hotspot covering the screen"
    Can you also please explain the "Hotspot covering the screen" part. I would love to learn how to do that too in case i need it elsewhere. If i make a hotspot covering the screen, won't it overlap with my sprite hotspot buttons?

    Thank you

  • 1) To have it only work at certain times, you'll need to use a variable to determine when it should work.

    Attach a Variables component to the same sprite/Hotspot and define a single Bool variable named e.g. "Can Click Away". Replace the ClickOffHotspot script above with this:

    using UnityEngine;
    using AC;
    
    public class ClickOffHotspot : MonoBehaviour
    {
    
        public ActionList actionListToRun;
    
        private void Update ()
        {
            if (GetComponent <Variables>().GetVariable (0).BooleanValue && Input.GetMouseButtonDown (0) && KickStarter.playerInteraction.GetActiveHotspot () != GetComponent <Hotspot>())
            {
                actionListToRun.Interact ();
            }
        }
    }
    

    Attach it to the same Hotspot, and then set the Variable's value (using the Variable: Set Action) to True when you want it to take effect.

    2) You said in your original post "I don't want to use a button hotspot to close it", which I took to mean a Hotspot covering the screen, i.e. making the whole screen one large Hotspot so that clicking it runs the "exit" interaction.

    This is a viable method, since you can turn the Hotspot on and off using the Hotspot: Enable or disable Action. If two Hotspots overlap in a 2D game, you should find that the one closer to the camera in the z-axis "wins" over the other one.

  • edited May 2020

    Hello,

    when i said "I don't want to use a button hotspot to close it" i meant an actual graphical button on my sprite with a hotspot which closes the popup. But it was lost in translation. :-)
    OMG i didn't know about the priority of the hotspot on Z axis. My problem is solved using that technique, it's exactly what i wanted, thank you so much.

    Only out of curiosity and because a reader here might need this and learn something, i tried what you suggested about solution 1) but can't make it work.
    I replaced the code, i successfully managed to use a variable to determine when it should work and take affect but...
    Now, the pop up sprite appears on screen, the code then becomes active but wherever i click (including inside the sprite puzzle buttons and area) the code closes and hides the sprite and so the puzzle.

    What am i missing?

    Here is my sprite inspector.
    https://imgur.com/0jKESuX

    Thank you

  • I was assuming that the Hotspot was covering the whole object - so clicking off the Hotspot would be equivalent to clicking anywhere except the whole sprite.

    You could adapt the code to instead check if it's over a collider that covers the sprite.

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.