- Home /
How to make player characters change color when only hit by specific colliders?
I have a script that lets my player characters change to red when collided with and then fade back to normal color quickly thereafter. However, it does this with every collider collision, when I really only want it to happen when collided with 2 specific colliders. How would I write the script to detect specific colliders/triggers collision?
Here's the script:
public class playerflashonhit : MonoBehaviour {
SpriteRenderer sr;
void Start(){
sr = GetComponent<SpriteRenderer>();
}
void OnCollisionEnter2D(Collision2D collision){//whenever we hit something
sr.color = new Color(2,0,0);//set this object's red color to 200 percent
}
void Update(){
//linear interpolation brings two values closer together proportional to a given third value(time)
sr.color = Color.Lerp (sr.color,Color.white,Time.deltaTime/1.5f);//slowly linear interpolate. takes about 3 seconds to return to white
}
}
Answer by Captain_Pineapple · Apr 26, 2018 at 06:58 PM
You can either give those specific objects a destinctive name and or you can give them a specific tag. Then you can check the following:
if(collision.transform.tag == "youSpecificTag")
{
//do color stuff
}
or:
if(collision.transform.name == "specificName")
{
//do color stuff
}
i hope this is correct and helps. I did not doublecheck it by trying this code.
Answer by Velociration · Apr 26, 2018 at 07:28 PM
So I tried this, since I have two players, I wrote the script like this:
void OnCollisionEnter2D(Collision2D collision)
{//whenever we hit something
if(collision.transform.tag == "Player2"){
sr.color = new Color(2,0,0);//set this object's red color to 200 percent
}
if (collision.transform.tag == "Player1") {
sr.color = new Color (2, 0, 0);
}
}
and that works, however, it works even when the players simply run into each other. What I want is for the children trigger colliders of the players to cause the color change. So I put new specific tags on those colliders called "DamageHitBox". However, this doesn't work. It doesn't seem to detect the children colliders and their specific tag.
And checking for the collision.transform.tag == "DamageHitBox" did not result in the desired behaviour? or is simply nothing happening?
In case you are not sure what collider you acutally are hitting you should add some debugging lines Like:
Debug.Log(transform.name + " just hit the collider tagged as: " + collision.transform.tag);
This way you can clarify what is happening.
Your answer
![](https://koobas.hobune.stream/wayback/20220612161600im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trigger not being activated? 1 Answer
How do I have a game object access the enums on another game object on collision? 1 Answer
Need help implementing a damage reduction 1 Answer
OnTriggerEnter2D is triggering when objects do not collide 1 Answer
trying to reverse 2D player's gravity after colliding with specific object 1 Answer