• 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 Velociration · Apr 26, 2018 at 06:30 PM · 2d-platformertriggersdamagecollision2dcolor change

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
 }

}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · 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 Captain_Pineapple · Apr 26, 2018 at 07:37 PM 0
Share

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.

avatar image
1

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.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Collider that restarts the game? 1 Answer

2D Character slows down in a trigger 2 Answers

How to activate all GameObjects named ,,GroundCollider'' by touching ,,GroundTriggerCollider'' and deactivating when no longer touching ''GroundCollider"? 1 Answer

Subtract value from one script to another 1 Answer

Player not taking damage on collision with enemy 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