Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Monkeyjesus · Feb 23, 2020 at 10:28 PM · collisioncolor changegraduallygradual

Gradually Change Color from Original to Red with Each Collision

I'm trying to change the color of the player object everytime it collides with an obstacle. I want to make it so that each time it collides, it becomes more red, however, I don't even know where to start. Here is my current CollisionTracker script.

 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     PrintDamage();
 }

 private void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.CompareTag("Obstacle"))
     {
         damagePercentage += 1;
         Debug.Log("Collision");
     }
 }

 public void PrintDamage()
 {
     if (damagePercentage <= 100)
     {
         damageText.text = $"Damage: {damagePercentage}%";
     }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JoltenDev · Feb 24, 2020 at 06:50 AM

You can use this https://docs.unity3d.com/ScriptReference/Material.SetColor.html :)

Comment
Add comment · 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 Monkeyjesus · Feb 24, 2020 at 08:35 PM 0
Share

Is there a way so that the player gets a little more red each time it hits an obstacle, rather than the player just turning red immediately?

avatar image JoltenDev Monkeyjesus · Feb 25, 2020 at 01:06 AM 0
Share

You could turn the colors you want into variables using this, https://docs.unity3d.com/ScriptReference/Color-ctor.html, and then use those variables in the setcolor script. You'll have to find out the float values of the colors you want, and i'm not good with color palettes and integrating them into rgb values, so I won't be much help telling you how to find the colors you want, but i'm sure there's some article out there that can provide you the information you need. :)

avatar image Fragsteel Monkeyjesus · Feb 25, 2020 at 10:39 PM 0
Share

Here's some pseudocode that should do something like it:

 Color _baseColor;
 float damagePercent = 0f;
 
 void Start()
 {
      _baseColor = my$$anonymous$$aterial.GetColor("myColor");
 }
 
 void OnCollisionEnter()
 {
     float damagePercent += 0.05f; //0.05 is arbitrary, but the point is to add the amount you want the 'redness' to increase.
     Color newcolor = _basecolor + (Color.Red * damagePercent);
     my$$anonymous$$aterial.SetColor("myColor", newcolor);
 }

Does that help? Interacting with shaders has a bit of a learning curve. I'll assume you know how to get the proper material (my$$anonymous$$aterial) and color name (myColor) but feel free to post if you need more guidance.

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

186 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 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 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 do I stop characters from standing on top of each other 1 Answer

How to change the color of the ball after hitting square? 5 Answers

Changing skyboxes? 1 Answer

How to change the material color of an Instatiated prefab when the player enters the collision area and presses a button? 0 Answers

change color of a cube having touched by Line Renderer 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