- Home /
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");
}
}
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.
switch() is almost never the right solution. You typically want to use polymorphism ins$$anonymous$$d. In this particular case, it sounds like the reaction to the effect should be tagged onto the thing causing the effect. See below answer.
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!
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.
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 ins$$anonymous$$d of a capsule collider?
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 $$anonymous$$e (allowing the projectile to do the work), but it should work with your CharacterController.
Wait but how does Dan Script help with doing more than one animation depending on the collision that i was hit with?
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)
}
}
}
Where is the PlayerController class co$$anonymous$$g from?
Would Send$$anonymous$$essage be better than onCollisionEnter?Because i heard that its slower than OnCollisionEnter.
@carrollh It's psuedocode... I don't know what I$$anonymous$$illed$$anonymous$$enny's player class is called, so I just named it something that made sense.
Send$$anonymous$$essage 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, ins$$anonymous$$d 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.
So in your opinion should i do sendmessage or OnCollisionEnter?Is Send$$anonymous$$essage that slow or just a bit slow?
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 Send$$anonymous$$essage isn't better either.
You will need to use OnCollisionEnter to deter$$anonymous$$e 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 Send$$anonymous$$essage... 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.
Your answer
Follow this Question
Related Questions
simple onCollision and onTrigger problem 1 Answer
Camera Collision Not Working 0 Answers
How to speed up? 3 Answers
Maintain velocity after impact with kinematic rigidbody 1 Answer
Fast object on collider not working 1 Answer