• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by IKilledKenny_2 · Jan 23, 2015 at 06:24 PM · collisionrigidbodycolliderontriggerentertags

100 OnTriggerEnter Or Tags each player too much?

Hello Unity3D.I have a question about the OnTriggerEnter and Tags.Is having 100 OnTriggerEnter and Tags on a player too much?The reason why i ask this question is the fact that i want to make the character that get hits depending on the tag that they character gets hit with plays a certain animation.For example.my character gets hit with a fireball and the fireball has a tag name fire.Therefore the character that gets hit with the fireball that is tagged fire the character plays animation called "fire hit"and if the character was hit with a water balloon and the water balloon was tagged water the character that got hit with the water balloon would play an animation called "water hit".Is there a way that i can make one script that can play OnTriggerEnter that depending on the tags of the players or objects that the character gets hit with plays a certain animation depending on the tag.If so.Can anyone please tell me how?

P.S This is an example

 #pragma strict
 
 var setOnFire : ParticleSystem;
 var player :Transform;
 var speed = 30;
 var pushPower = 2.0;
 var anim: String;
 var cam: Camera;
 
 function OnTriggerEnter (Col : Collider)
  {
      if (Col.tag == "Fist")
      {
          // Default hit animation
          var anim : String = "Hit1";
          
          // Animation related to player's animation
          if(Col.animation.IsPlaying("Gigantic_FireBall_Part_2"))
          
           anim = "Shinzuroken_Hit";
          
      
          animation.Play(anim);
          cam.animation.Play("Hit Animation Camera");
          cam.animation["Hit Animation Camera"].speed = 2;
          setOnFire.Play();
      }
  }
  
  function OnTriggerExit (other : Collider)
  {
          
          if (other.tag == "Player")
          {
          cam.animation.Play("Normal Camera");
 
          
          
          }
          
 }         
Comment

People who like this

0 Show 3
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 meat5000 · Jan 23, 2015 at 06:30 PM 0
Share

Don't get what you mean. You need to clarify.

avatar image tanoshimi · Jan 23, 2015 at 07:28 PM 0
Share

I suggest you learn about switch statements. Given that each object can have only one tag, it makes it pretty clean and efficient to divide your trigger logic in only a single OnTriggerEnter function.

avatar image jwatte-imvu · Jan 26, 2015 at 08:04 PM 0
Share

switch() is almost never the right solution. You typically want to use polymorphism instead. In this particular case, it sounds like the reaction to the effect should be tagged onto the thing causing the effect. See below answer.

3 Replies

  • Sort: 
avatar image

Answer by MakinStuffLookGood · Jan 23, 2015 at 06:59 PM

So you want to have 100 scripts that have void OnTriggerEnter() defined in them and have all of those attached to one player, with 100 tags individually handled by the 100 scripts?

Yes, that is absolutely WAY too much and is certainly not now the Tag system was intended to be used.

Now, if you give an explanation of what exactly you're trying to do, perhaps we can help you find a solution that is cleaner than 100 frivolous scripts and tags!

Comment
SnStarr
Josh707

People who like this

2 Show 0 · 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

Answer by carrollh · Jan 24, 2015 at 12:56 AM

So what are you using to control your animations? If it's an AnimatorController, then the naive approach would be to add a Boolean to it's base layer for each animation you want to use. Then in the script on your fireball or whatever you could do this.

 GameObject player;
 Animator playerAnim;
 
 void Start() {
      player = GameObject.FindGameObjectWithTag("Player");
      playerAnim = player.GetComponent<Animator>();
 }
 
 void OnCollisionEnter(Collision other) {
      if(other.gameObject.tag.Equals("Player")) 
      {
           // assuming you have an AnimationController with some
           // animation state that gets triggered when the 
           // layer's Boolean Burning is true.
           playerAnim.SetBool("Burning", true);
      }
 }

With this, the things you are hitting the player with don't even need tagging.

Comment

People who like this

0 Show 3 · 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 IKilledKenny_2 · Jan 24, 2015 at 01:48 AM 0
Share

Oh im using Legacy .in order to do animations for my characters Also OnCollisionEnter never works for me.Does it have to do with the fact that im using a character controller instead of a capsule collider?

avatar image carrollh · Jan 24, 2015 at 01:54 AM 0
Share

Ah. Character Controllers have a capsule collider already. Look here: http://answers.unity3d.com/questions/7671/guidelines-for-using-rigidbody-collider-characterc.html

Also, Dan's script below might work for you. It's the same overall concept as mine (allowing the projectile to do the work), but it should work with your CharacterController.

avatar image IKilledKenny_2 · Jan 24, 2015 at 02:24 AM 0
Share

Wait but how does Dan Script help with doing more than one animation depending on the collision that i was hit with?

avatar image

Answer by DanSuperGP · Jan 24, 2015 at 01:32 AM

It's not a good way of doing things. A much better idea would be to have your projectile have an OnTriggerEnter script that checks if it hits a player, and if it does informs the player that it was hit by a projectile and passes the relevant information about what kind of projectile it is and what animations it should play in response.

Something like

 void OnTriggerEnter( Collider other)
 {
  if(other.CompareTag("Player")
  {
      PlayerController pController = other.GetComponent<PlayerController>();
      if( pController != null)
      {
             pController.HitByProjectile( this.projectileDamage, this.ProjectileType)

      }
  }

}

Comment

People who like this

0 Show 8 · 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 carrollh · Jan 24, 2015 at 01:59 AM 0
Share

Where is the PlayerController class coming from?

avatar image IKilledKenny_2 · Jan 24, 2015 at 05:42 PM 0
Share

Would SendMessage be better than onCollisionEnter?Because i heard that its slower than OnCollisionEnter.

avatar image DanSuperGP · Jan 24, 2015 at 06:29 PM 0
Share

@carrollh It's psuedocode... I don't know what IKilledKenny's player class is called, so I just named it something that made sense.

SendMessage works just fine... but I wouldn't say it's better. It is slower, it has no error checking, you can only pass in one argument.

The important thing about this kind of technique is, instead of have a monstrous if/else or switch statement on the player which is very hard to maintain... you can put the relevant information on the projectile, like the name of the animation to play.

avatar image IKilledKenny_2 · Jan 24, 2015 at 07:18 PM 0
Share

So in your opinion should i do sendmessage or OnCollisionEnter?Is SendMessage that slow or just a bit slow?

avatar image DanSuperGP · Jan 26, 2015 at 05:24 PM 0
Share

In my opinion you should not do a giant if/else (or switch) statement in OnCollisionEnter based on tags. A giant if/else (or switch) statement called by SendMessage isn't better either.

You will need to use OnCollisionEnter to determine when your projectile hits either way...

The important thing I'm trying to tell you is rather than having a giant if/else defining how to react to every different kind of projectile... the projectile should contain the important information on how to react... and inform the player when it hit's ... I'm a fireball... you should react with the "hit by fireball" animation.

You can do this with SendMessage... you can do it by getting a component on the player that was hit and then calling a function... you can do it other ways...

But the important thing is... having the player have a giant list of different if/else and using 100 tags for different projectile types is very hard to maintain and simply not a good design.

Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

simple onCollision and onTrigger problem 1 Answer

OnTriggerEnter works, OnTriggerExit doesn't 3 Answers

Audio.PlayOneSHot won't work? 0 Answers

Detect collision point?? 1 Answer

how do i correctly use a box collider 0 Answers


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