How to code triggers to react differently for different character options?

I am trying to code different triggers so that some triggers will only affect certain characters. For example, the water character will evaporate(die) in fire/lava, and the fire character will extinguish(die) in water.

I was planning on making different triggers for each element so that they affect other characters differently. Although I have a plan, coding the triggers to react differently to each character is new to me. While I am familiar with C++ and Java, this is my first project using C# and coding in Unity.

My coding plan is to set a public variable to a different char/int value per water/fire character, and then have it that if the public variable == [insert appropriate value] then the character will die. The problem is I do not know how to format or code that, as I’ve just copied the sample code for triggers from Unity’s Sample 2D platformer.

Could someone give me some tips/guides/sample code/etc?

Well, I am new to this as well, but immediately Interfaces come to mind.

1 Like

A better place to start on your general scripting adventure is the Getting Started forum. There’s also plenty of tutorials to go through on the Unity Learn site.

1 Like

Sounds quite advanced what you’re doing, are you an experienced Unity developer?

So yeah just have a property tied to the character script like:

public string elementType;

in the inspector, input what type they are ‘Water’, ‘Fire’, ‘Earth’ etc.

Then in the DeathZone script, just write conditional statements in the OnEnterTrigger2D method.

if (player.elementType == 'Fire') {
// Do this
}

if (player.elementType == 'Water') {
// Do this
}

Might also be a good idea to fine tune your collission Matrix, if you only want player objects to interact with ‘DeathZones’.

1 Like

No, I’m only experienced with C++ and Java from classes that I am/was taking.

Oh, that was way simpler than I was thinking! I thought that I would have to create separate classes within that file!

Gotcha, and l am actually watching some of those tutorials rn! Although, it might be a while before I reach to where I need and this is for a college project, so I will post this in the Getting Started forum if I still need help! Thanks!

1 Like

Awesome! Thanks for the advice!