- Home /
Multiple OnTriggerEnter events on same Script
Hello guys, I am doing a 2D game where the main objective is to avoid being caught on certain traps. It's quite similar to all the "I wanna be the ..." series, which has a lots of events depending where your character is standing.
What I've been doing is making lots of scripts where in each one the function "OnTriggerEnter2D" has a different code, cause all the events are different depending on what I want to do. The issue is that is not efficient, cause it's all harcoded, for example:
void OnTriggerEnter2D(Collider2D character){
float xA = character.transform.position.x;
float yA = character.transform.position.y;
character.transform.position = new Vector2(xA-35,yA);
}
That moves my character specifically 35 to the left on the x axis, but what happens if eventually i want to do another function that moves my character on another direction? I don't want to create another script to make another OnTriggerEnter2D function...
So the question is: Can I do multiple times a certain function with different codes in JUST ONE Script? What's the best way to do what i want?
Thanks!!
No, you can't have two methods named OnTriggerEnter2D in one script. You could setup a string variable (or an Enum) and give your effect a name, then in OnTriggerEnter use a switch/case
Answer by largow · Apr 07, 2014 at 11:22 PM
Thanks, but that's solves a just part of the problem. After some research I found out that a good idea to implement this is doing a script for the character, where the OnTriggerEnterFunction has a Switch/Case structure inside of it ( like getyour411 said. On each case you write the different codes.
void OnTriggerEnter2D(Collider2D collider){
//PRE: YOUR COLLIDER TAG HAS TO FINISH WITH A NUMBER, OTHERWISE IT WON'T WORK THIS WAY.
string aux = collider.name;
int index = aux.IndexOf('_'); //My collider's name follow this: collider_# // //where # is an int
if(index > 0)
//get the chars after thespecial character I used
aux = aux.Substring(index+1, aux.Length-(index+1));
//Here i get the number, starting 1 after my special character
//and going to the lenght of my string aux - the positions
//i ve moved to reach the index.
int colliderNumber = int.Parse(aux);
switch(colliderNumber){
case 1:
Console.WriteLine("JUST TOUCHED COLLIDER 1");
break;
case 2:
Console.WriteLine("JUST TOUCHED COLLIDER 2");
break;
case 3:
Console.WriteLine("JUST TOUCHED COLLIDER 3");
break;
default:
Console.WriteLine("Default");
break;
}
}
//If you don't want to name all your colliders with a number at //the end, you could use some if/else structures:
void OnTriggerEnter2D(Collider2D collider){
if (collider.name == "Wall") {
//Do something with Wall or your character
} else {
if (collider.name == "Floor") {
//Do something with floor or your character
}
}
Answer by Ermarrero · Apr 07, 2014 at 02:15 AM
Try something like below, let me know if it works:
public Vector2 characterMoveDirection;
public bool dontChangeX; //Just in case you do not want to change the characters X position.
public bool dontChangeY; //Just in case you do not want to change the characters Y position.
void OnTriggerEnter2D(Collider2D character)
{
Vector2 characterPos = new Vector2(character.transform.position.x, character.transform.position.y);
if (dontChangeX) character.transform.position = new Vector2(characterPos.x, characterMoveDirection.y);
else if (dontChangeY) character.transform.position = new Vector2(characterMoveDirection.x, characterPos.y);
else character.transform.position = characterMoveDirection;
}
Answer by RyanZimmerman87 · Apr 08, 2014 at 03:08 AM
I would probably just add a few variables on your script which determines what happens in your OnTriggerEnter.
I would probably just use some int variables rather than bools for something like this. I find ints to be a lot more effective to control more advanced sequences of logic than trying to combine a ton of bool conditions.
So for example you could have something like this on your collider objects script:
//set public int variables to determine the behavior
//you make them public so you can easily customize
//every new collider you add in the inspector
//controls which direction to shoot player
//0 is up, 1 is right, 2 is down, 3 is left
public int directionInt;
public int distanceInt;
void OnTriggerEnter2D(Collider2D character)
{
//example of up
if (directionInt == 0)
{
character.transform.position.y += distanceInt;
}
//example of right
else if (directionInt == 1)
{
character.transform.position.x += distanceInt;
}
//example of left
else if (directionInt == 3)
{
character.transform.position.x += -distanceInt;
}
}
It sounds like you already have a good handle on this stuff but I think using a couple int variables would be the easiest way to handle this as long as you remember what each variable number means.
You could also have another int to determine the initial collider behavior and divide your OnTriggerEnter logic based off that. For example you could have a:
//0 moves them a different direction, 1 spawn enemies, 2 gives them some debuff, etc.
public int colliderTypeInt;