Unity On Collision Enter 2d



Things like this can really be frustrating, being that upgrading one of 2D games made to a higher version of Unity, I guess you would expect things to break, but after 1.5 hrs of debugging why my 2D collisions just don't work, I simply tick the box 'Use Full Kinematics Contacts' and it works like it should. Well, OnCollisionEnter2D (with parameter Collision2D) is one such method. It's a method that is fired (called) by Unity whenever it detects a collision between the object to which this script is attached to, and any another gameObject. Note the Collision2D col parameter we provided. In this tutorial we discuss adding colliders on our objects so that we can detect collision within our script.SUBSCRIBE: https://bit.ly/2Js78lEI.

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.

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

Detect Collision Unity 2d

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

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:

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 Collision Enter 2d

I’ve been tinkering with Unity’s 2D tooling since it was officially released in v4.3. With each new version we see some improvements in the 2D tools and workflow, but alas it’s far from perfect and as a result getting things to work the way you want is all about trial and error.

Unity On Collision Enter 2d Using

One thing I have learnt quickly about Unity is that there are many ways to approach a problem, each with their own pros and cons. Sometimes you need to get creative with your solutions. Player colliders in Unity are one such area that that can be a struggle. Some of the problems commonly experienced when making tile based 2D games is player objects getting stuck on floors, walls and ceilings.

Below I will outline some of the problems, why they occur, and then share with you my approach, which although isn’t bulletproof, works for me.
Ray Casting is another approach which can work well, but has its own challenges to overcome. It will not however be the focus of this post.

Note* I am using Mario sprites simply for illustrative purposes. The real Mario was created long before Unity, and obviously used a different (better) system.

Getting stuck on the floor

Using a box collider with a simple Mario sprite seems like the perfect way to handle collision, but unfortunately Unity struggles with this setup and its quite common for your players collider to get caught on floor tiles that also have their own box colliders.

This has nothing to do with pixel spacing between tiles and will happen even if you have your tiles snapped together perfectly. I believe the problem comes from floating point values which are the source of many problem in various parts parts of the Unity workflow.

In some cases it may be possible to create one massive box collider for the whole floor or platform, this is a pretty limiting approach though, especially in a tile based world which can have many platform sizes.
Consider also that in a game like Mario, blocks are destructible, so the collider must be removed with the block. This would not be possible with one large collider spanning the size of a platform.

Circle Colliders

Using two circle colliders is a popular setup which I have seen used quite often in other tutorials and example projects. And though it eliminates the problem with floor stickiness, it creates new problems due to limited accuracy when jumping onto platforms (the player will roll off edges). Once again, it depends on your games requirements. If your game is a downhill runner, then sure circle colliders might do the trick, but to use a Mario platformer as an example requirement, you would need something more robust.

Hybrid Collider Setup

Which leads us to the setup which I have found works for me. It is a combination of box and circle colliders positioned in such a way to avoid obstacles getting ‘caught’.

You will need to test the sizing s against walls and floors, and make little adjustments till it feels right. Remember folks, if it feels right it is right. 😉

Remember, this approach isn’t fool proof, but it got me as close as I needed without using ray-casting.
One final step which is quite important. Add a low friction aka ‘slippery’ Physical Material to your box collider. This will stop it getting caught on walls when you are pressed up against them and attempt to jump.

Creating a PhysicalMaterial for a 2D game is easy, just right click in your project panel, and select create > Physical 2D Material

Give the Physical2D material a name like ‘Slippery’ then double click it and lower the friction settings to 0.

Finally to apply it to your player’s box collider, click on your player and drag the Slippery material to the ‘Material’ slot under the box collider section in the players property panel.

Enter

Join me on Patreon to get exclusive access to source code and valuable gamedev info.

If you have any questions or suggestions drop them in the comments below.

Adios.