Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by largow · Apr 07, 2014 at 01:58 AM · eventevent-handling

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!!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Apr 07, 2014 at 02:18 AM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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                
                     
       }
                         
                         
    }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I replace the behaviour of a GUILayout.scrollView on MouseWheel event? 2 Answers

Fire an event when gameObject has been renamed? 2 Answers

checking for keyboard input with exceptions 0 Answers

Event is not being implemented from interface 0 Answers

OnPointerDown vs OnMouseDown 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges