Forum rules - please read before posting.

One trigger for enter and exit

Hello,

now I use two triggers, one for enter which show the hotspot and then one trigger for exit on the same place which hide the hotspot. I would like to have just one trigger and when I enter it makes some actions, when I leave it makes another actions.

I didn't find the solution here so far.

Thank you for any help.

Comments

  • edited August 2016
    Ummm I must admit I can't remember if you can do that with AC triggers, but you can use a regular Unity trigger (or a raycast, although its more expensive). Just slap the following code into the AC trigger (or any object with a collider with the trigger option checked)

    [SerializeField]
    private GameObject myHotspot;

    //remove the collider variables if you don't care who enters the trigger. If you do remove them, also remove the if conditions.
    void OnTriggerEnter(Collider other) 
            {
                if (other.tag == "Player")
                {
                    myHotspot.SetActive(true);
                }
            }

            void OnTriggerExit(Collider other)
            {
                if (other.tag == "Player")
                {
                    myHotspot.SetActive(false);
                }
            }

    Just don't forget to set what object you want to enable/disable in the unity editor before your run it or it'll return an error.
  • edited August 2016
    In hindsight, it would be nice if AC triggers could have separate action list for enter and exit. Would't be hard to code a custom one though, just add the following to the previous code:

    [SerializeField]
    private ActionListAsset toDoOnEnter;
    [SerializeField]
    private ActionListAsset toDoOnExit;

    Then, inside the OntriggerEnter and OntriggerExit if statements respectively, add (or replace the SetActive statements) with the following :

    if (toDoOnEnter != null)
    {
    AdvGame.RunActionListAsset(toDoOnEnter);
    }

    if (toDoOnExit != null)
    {
    AdvGame.RunActionListAsset(toDoOnExit);
    }
  • Ah, and don't forget to add at the top of your script: 

    using AC;

    Else it will give you warnings.
  • edited August 2016
    Oh thank you, I really dont know how to use this code, I am not big scripter :)

    I can imagine something like select in Trigger type: coutinuous or enter/exit and when I hit the enter and exit option there will be possiblity to assign action for each.

    image

  • ummm.... In unity, in the project window, right click some empty spot, in the new context menu which appears go to create, then choose C# Script. Unity will create a base template script for you. Then all you need to do is paste the code right after the line which says: 

    //use this for initialization

    In fact you can just replace that line. Just highlight the line with the mouse like in any text editor and then paste on top. Then to use the script all you have to do is drag it from the project window and drop it on top of whatever object you want it to affect. In case you just want to do the last step I uploaded the whole script here, all you have to do is move it to a folder inside the Asset folder in your Unity Project. This is the version which takes Actionlist assets though. once the script is attached to an object you will get some slots to drag and drop your action list assets (similar to the way hotspots work, just click the hotspot in which you dropped the script and you will get the options in the Inspector tab). 

    If you want to make any changes to the script, just right click the file while in windows explorer (or whatever people use in OSX) and select edit, so you can use notepad or some other simple text editor. If you want to know how to use unity triggers you can watch the basic Unity tutorial here. I recommend you to do so, because lots of assets or scripts out there will need you to do this steps by yourself, generally, AC is an exception.

    Anyways, don't be afraid to tinker with that, it's actually something which you'll probably be doing a lot. You don't really need to be a coding master to use code, a lot of people just copy paste what they find in the internet. Remember that if some code you got doesn't work or errors out all you have to do is delete it or find a fix in the internet and no further harm was ever done. Anyway, happy development!
  • Thank you for this long explanation, I didnt expect that :). I can say that I am not a newbie in programming, but in Unity I am. Never did any scripts in C# so I need to get into it.

    The script looks great, but still here is the error message: The script 'CustomTrigger.cs' must derive from AC's Action class in order to be available as an Action.

    And how it works when I have assigned AC_trigger script in a Trigger, probably I need to use only AC_trigger or your solution, right?


  • edited August 2016
    That script isn't an AC action, so if you put it in AC's Actions folder it won't work, sorry. All you have to do is manually drag it and drop it on top of any objects you want the script to affect. Thats the general work flow in unity, it's hard for people who have worked with other platforms to get used to it at first, but the usual workflow is: you make a make a script, and then just slot it/drop it on an object (be it in the hierarchy or directly on the scene window, or in the inspector), everything in unity works with real objects mentality. For example if you make an empty exposed variable (unassigned, like in my script), Unity will simply put a slot or field in the inspector tab where it expects you to drop something in it (in case it's text or numbers it ill put a text box for you to type). But anyway, once you have a script attached to an object, all you have to do is click that object (hierarchy or scene view, even the library tab), then you will get the script's options in the inspector tab.
  • Thank you, now the error has disappeared, but when I go to the trigger's position, the scripts do nothing. Does this script work on every object and detects the player?
  • The object needs to have a collider (any type, box, mesh, capsule, etc) and the collider needs to have the IsTrigger toggle checked.The player also needs to have a collider, and the player needs to be tagged as "Player", but if you are using AC's character wizard, then it's already tagging your player as "Player". You can easily make a trigger by creating a regular cube. making it the size you need, then deleting(or turning off) the mesh rendered, just leave the collider on, and mark the collider as a trigger. The difference between a collider marked and not marked as a trigger is that the collider not marked as a trigger will obstruct other objects path, while the collider marked as a trigger won't collide with anything, it will just register if the collision happened or not. In case that you are using custom tags for your objects or player you can just open the script and change the tag used in the checks. 

    Also the script needs you to tell it what to do when there's a collision, in this case to make things more AC, I told the script to ask for Action list Assets. To create one go to the library tab and right click in an empty space, go to create and look for Adventure creator, there you will have the option to create an Action list Asset, they're the reusable version of regular action lists. Once created, just double click it and fill it with the actions you want. Then drag and drop it on the inspector tab in the slots next to the words "to Do On Enter" and "to Do On Exit".

    I made it this way so that you can use the same trigger for anything you want. not just hiding or showing hotspots.
  • Now I was testing it, and wanted to ask if here is any differences between the 3D and 2D. That was the problem, I am sorry for that, I forgot to mention that I am usign 2D. So I have to use OnTriggerExit2D and Collider2D.

    Thank you so much for your patience, now it works well.
  • Hey, I'm glad you got it working and sorry I forgot to ask if you were using 2D or 3D. Anyways, happy developing!
  • Thank you! I am pretty sure, that someday I will need some help again :)
  • edited August 2016
    By the way, I posted the code in the Newly created Adventure creator Wikia. The code is slightly altered, so you can change the name of the tag to detect, and do so directly in the inspector tab. It also has mouse tool tips to remind you what each field does, in case you forget. If you want to add those features to the script you already have, you can see the code here. Anyways, if you ever have another question just fire away. Me or someone else in the forums will be happy to help.
  • edited October 2018
    Hi all, I know it's been a while since this question was asked, but I was also looking for a solution for this and wanted to share what I did.
    I don't know if there is a particular problem doing this, but I just added another AC_Trigger script to the Trigger object and set up that to handle exit.

    For a little more detail I'll outline the steps:
    1) Create a Trigger in the normal way
    2) Setup the Trigger script to handle enter, and set up whatever scripts you like
    3) Use Add Component, and select AC_Trigger
    4) Setup the trigger script on the new Component to handle exit, and setup whatever scripts you like

    I imagine there may be some objects that wouldn't handle this so well, but for my particular case (setting object visibility) it works a treat!
  • I've been using this method for ages and it works great ;) Two AC_trigger components and bob's your uncle ;)

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.