Unity On Trigger Enter 2d

Posted on  by 



  1. Unity On Trigger Enter 2d
  2. Unity On Trigger Enter 2d Images
  3. Unity On Trigger Enter 2d

Sent when another object enters a trigger collider attached to this object (2D physics only). Further information about the other collider is reported in the Collider2D parameter passed during the call. Trigger events will be sent to disabled MonoBehaviour s, to allow enabling Behaviour s. These options should cover both 2D and 3D, just remember that the 2D version has a 2D appended at the end of both the method name and its parameter (e.g. OnTriggerEnter(Collider) becomes OnTriggerEnter2D(Collider2D)). Is it a trigger? Does at least one of the objects have Rigidbody attached? When will the trigger occur: Start Destroy Update Enable Disable TriggerEnter TriggerExit CollisionEnter CollisionExit MouseUp MouseDown MouseOver MouseExit. Trigger On Update. Trigger Enter 2D. Trigger Exit 2D. Collision Enter 2D. Collision Exit 2D. On Mouse Up As Button. On Particle Collision. The action to be taken.

This section explains how to make things happen in the Dialogue System.

Note: If you are making a 2D project, see Enable Physics2D Support in Unity 2018+.

The Dialogue System Trigger component has three main parts:

  • Trigger: The event that causes the Dialogue System Trigger to fire.
  • Conditions: States that must be true for the Dialogue System Trigger to perform its actions.
  • Actions: Things the Trigger should do, such as starting conversations or setting quest states.

Trigger

You can set the Trigger dropdown to these values:

Trigger Description
On Use The player’s Selector or Proximity Selector sent an OnUse message to the GameObject, or the Dialogue System Trigger’s OnUse() method was called manually in a UnityEvent or script.
On Start The component started (e.g., at scene start).
On Enable The component was enabled.
On Disable The component was disabled.
On Destroy The component was destroyed.
On Trigger Enter The component received an OnTriggerEnter message. To use this trigger, the component’s GameObject should have a trigger collider. You may want to set Conditions → Accepted Tags to restrict this trigger to GameObjects with specific tags such as Player.
On Trigger Exit The component received an OnTriggerExit message.
On Collision Enter The component received an OnCollisionEnter message.
On Collision Exit The component received an OnCollisionExit message.
On Bark Start The GameObject started playing a bark (one-off line of dialogue).
On Bark End The GameObject finished playing a bark.
On Conversation Start The GameObject just started as a primary participant in a conversation.
On Conversation End The GameObject just ended a conversation.
On Sequence Start The GameObject just started as a primary participant in a cutscene sequence. This event is not called for in-conversation sequences unless you’ve ticked the Dialogue Manager’s Subtitle Settings → Inform Sequence Start And End checkbox.
On Sequence End The GameObject just ended a sequence.

Conditions

Conditions allow you to specify states that must be true for the Dialogue System Trigger to fire, including:

Condition Type Description
Lua Conditions Lua expressions such as checking the value of a variable.
Quest Conditions Required quest states.
Accepted Tags For OnCollision and OnTrigger events, the other GameObject must have one of these tags. If Accepted Tags is empty, all GameObjects are allowed.
Accepted GameObjects For OnCollision and OnTrigger events, the other GameObject must be in this list. If Accepted GameObjects is empty, all GameObjects are allowed.

Actions

To add actions to perfom, click the Add Action button. You can add these actions:

Action Description
Set Quest State Sets quest and/or quest entry states. (See Quests.)
Run Lua Code Runs Lua expressions. Lua Variable['Actor'] and Variable['ActorIndex'] are set to the interactor's info. (See Logic & Lua.)
Play Sequence Plays a cutscene sequence. Sequence speaker is set to the interactor's info. (See Cutscene Sequences)
Show Alert Shows an alert message through the dialogue UI.
Send Messages Uses Unity’s SendMessage() method to send messages to targets.
Bark Plays a bark (one-off line of dialogue) on a GameObject.
Start Conversation Starts a conversation.
Set GameObjects Active/Inactive Works on entire GameObjects.
Set Components Enabled/Disabled Works on specific components of a GameObject.
Set Animator States Sets animator states on GameObjects. Useful to idle characters when conversations start.
OnExecute() UnityEvent Allows you to specify other actions using a UnityEvent.

Character GameObject Assignments

Note: Actor GameObjects used in runtime conversations don't have to be the same as the actors assigned to the conversation in your dialogue database! See below for an explanation.

Conversation Actor & Conversant

When you create a conversation in the Dialogue Editor, you'll assign an actor and a conversant from the dialogue database's list of actors. Typically, the actor is the player and the conversant is the primary NPC involved in the conversation. To view or change the conversation's actor and conversant, click on empty canvas space to inspect the conversation's properties.

Dialogue Entry Node Actor & Conversant

As you add dialogue entry nodes, the editor will automatically assign an actor and conversant to the node. The node's actor is the character who is speaking. The node's conversant is the character being spoken to. To change a node's actor or conversant, click on the node to inspect its properties.

Dialogue System Trigger Actor & Conversant

When you set up a Dialogue System Trigger, you can assign GameObjects to the Start Conversation > Conversation Actor and Conversation Conversant fields. These GameObjects can represent different actors than those assigned to the conversation in your dialogue database.

The conversation will use these GameObjects even if they don't match the actors assigned to the conversation. This allows you to reuse conversations for different characters – for example, reusing the same shopkeeper conversation for all shopkeepers in your game world. (Tip: In your dialogue database, you can create a generic actor named 'Shopkeeper' with a generic conversation such as: 'Hello, I'm [var=Conversant]. Welcome to my shop!' etc. Then reuse the same shopkeeper conversation for each village's shopkeeper and simply assign the specific village shopkeeper to the Conversation Conversant field.)

Conversation Actor

If you don't assign the Conversation Actor field, the Dialogue System Trigger will try to choose an appropriate GameObject:

  • If the trigger type is OnTriggerEnter/Exit or OnCollisionEnter/Exit, it will use the GameObject that collided with the trigger.
  • If the trigger type is OnUse, it will use the GameObject that sent the 'OnUse' message, typically the GameObject with a Selector or Proximity Selector component.
  • Otherwise it will look for a GameObject that has a Dialogue Actor component whose actor dropdown matches the conversation's actor. Failing that, it will look for a GameObject whose name matches the conversation's actor.

Conversation Conversant

If you don't assign the Conversation Conversant field, the Dialogue System Trigger will generally try to choose an appropriate GameObject:

  • It will look for a GameObject that has a Dialogue Actor component whose actor dropdown matches the conversation's conversant.
  • Failing that, it will use the Dialogue System Trigger GameObject.

Other Participants

If a dialogue entry node is assigned to an actor that is not the conversation's primary actor or conversant, it will look for a GameObject that has a Dialogue Actor component whose actor dropdown matches the node's actor. Failing that, it will look for a GameObject whose name matches the node's actor.

Bark On Idle

Use Bark On Idle to play barks on a GameObject at a random frequency between Min Seconds and Max Seconds. See the Barks section for more information about barks.

Range Trigger

Use Range Trigger to activate GameObjects and enable components OnTriggerEnter when specified Conditions are met. When OnTriggerExit occurs, the GameObjects are deactivated and components disabled. This component is useful to enable components such as Bark On Idle only when the player enters a trigger collider that represents proximity to the barker. You can also configure UnityEvents to run on enter and exit.

Trigger Event, Collision Event, Timed Event

The Trigger Event, Collision Event, and Timed Event components are general-purpose components that run UnityEvents on trigger collisions, regular physics collisions, and elapsed durations, respectively.

The Dialogue System Events component runs UnityEvents when Dialogue System activity occurs. Each foldout has a section of events that the component can handle when its GameObject receives the corresponding message, such as OnConversationStart. Note that many events, such as OnConversationStart, are only called on the two primary participants and the Dialogue Manager. If you want to run an event regardless of who the participants are, put the Dialogue System Events component on the Dialogue Manager.

You can also handle events in your own scripts by adding special methods described Scripting.

The Dialogue System provides an optional interaction system that can interact with GameObjects (e.g., NPCs) that have Usable components. You can add one of two components to the player:

Selector

The Selector component detects Usables by raycasting from a specified position, such as the mouse position or center of screen. When the player presses the use button, it sends an OnUse Message to the Usable. Note: In 2D mode, the camera must be orthographic.

Unity

Proximity Selector

The Proximity Selector component detects Usables when entering trigger colliders. When the player presses the use button, it sends an OnUse Message to the Usable.

OnUse Message

When the player targets a Usable and presses the Use Key or Use Button, the Selector will send an OnUse(Transform player) message to the usable GameObject. The Dialogue System's triggers, such as Dialogue System Trigger, respond to this message. Your own scripts can also respond to this message by adding an OnUse method such as:

Debug.Log('I'm being used by ' + player);

Usables also have UnityEvents that run when selected, deselected, and used. You can configure these events in the inspector.

Interactive conversations usually have a back-and-forth flow, where the actor says a line, then the conversant says a line, and so on. Barks, on the other hand, are one-off lines of dialogue that don't involve a player response menu or any back-and-forth. They're typically spoken by an NPC for atmosphere (e.g., 'Nice weather today') or to give the player an idea of the NPC's internal state (e.g., 'Reloading. Cover me!'). The content of barks can come from text strings or conversations.

Bark Conversations

To create a bark conversation, add all bark entries as children of the START node. In other words, the conversation tree will only be one level deep. When an actor barks, the Dialogue System will evaluate all children of the START node to generate a list of currently-valid entries. It will then choose an entry from this list and bark it. By setting Conditions on the bark conversation's dialogue entries, barks can be aware of the current game state. For example, an NPC could bark different things based on its health level.

Bark Priority

You can specify priority levels on your bark entries to prevent lower-priority entries from interrupting higher ones. To do this, add a custom Number field named 'Priority' to your dialogue entry. Higher values have higher priority. If you don't want to have to add this custom field on each entry, you can add it to the template on the Dialogue Editor Templates Tab. If the dialogue entry doesn't have a custom field named 'Priority', it uses a default of 0. When a bark is triggered, if the NPC is already barking, the Dialogue System will only interrupt it to play the new bark if the new bark's Priority is greater than or equal to the current bark's Priority.

Bark Display

Barks don't use the dialogue UI. Instead, barks are played through a bark UI on the barker, which usually appears as text over the NPC's head and/or as audio played through the NPC's audio source. The Dialogue UIs section covers bark UIs.

Bark Groups

Unity On Trigger Enter 2d

If you want only one barker in a group to show bark text at time, configure them as Bark Group Members by adding a Bark Group Member component to each. When one member of the group barks, the others will hide any active barks to reduce onscreen clutter.

If you tick a Bark Group Member's Queue Barks checkbox, then when another member is currently barking, it will queue its bark to play after the other member instead of playing immediately and hiding the other member's bark.

The Dialogue System supports 2D physics as well as 3D physics. In Unity 2018+, the 2D physics package (Physics2D) can be enabled or disabled, so the Dialogue System's code for Unity 2018+ doesn't assume that Physics2D is available in your project. To tell the Dialogue System that Physics2D is available if you’re using Unity 2018+, open the Welcome Window (Tools → Pixel Crushers → Dialogue System → Welcome Window) and tick Enable 2D Physics or select menu item Tools → Pixel Crushers → Common → Misc → Enable Physics2D Support.... If you're using Unity 2017 or older, this menu item will not be visible.

If you want to enable Physics2D support manually instead, select menu item Edit → Settings → Player, add the scripting symbol USE_PHYSICS2D as shown below:

Unity On Trigger Enter 2d

If you're having trouble getting the Selector component to detect your 2D Usables, please see Why isn't 2D detection working?.

If you’re using Unity 2017 or older, you can skip this step.

<< Welcome to the Dialogue System for Unity! | Dialogue UIs >>

Unity On Trigger Enter 2d Images

Comments

Unity On Trigger Enter 2d

  • 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]

    //remove the collider variables if you don't care who enters the trigger. If you do remove them, also remove the if conditions.
    {
    {
    }

    {
    {
    }

    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:
    private ActionListAsset toDoOnEnter;
    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);

    {
    }
  • 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.
  • 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




Coments are closed