Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 behnker · Aug 03, 2010 at 11:16 PM · meshplayerlerpfadehealth

Player Mesh fade from one color to another

Im trying to have a flash of a color every time the enemy is hit and then have it fade from blue to red. Here is my current code.

var hitPoints = 100.0; var deadReplacement : Transform; var dieSound : AudioClip; var colorStart1 = Color.blue; var colorEnd1 = Color.blue; var colorStart2 = Color.red; var colorEnd2 = Color.red; var playerMesh : Renderer; var duration = 1.0; var lerp = (Time.time - duration) / duration;

function ApplyDamage (damage : float) { // We already have less than 0 hitpoints, maybe we got killed already? if (hitPoints <= 0.0) return;

 if (hitPoints &gt;= 51){   
 playerMesh.material.color = Color.Lerp (colorStart1, colorEnd1, lerp);
 }

 if (hitPoints &lt;= 50){   
 playerMesh.material.color = Color.Lerp (colorStart2, colorEnd2, lerp);
 }

 hitPoints -= damage;



 if (hitPoints &lt;= 0.0)
 {
     Detonate();
 }

}

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
1

Answer by Peter G · Aug 04, 2010 at 12:08 AM

You didn't say what your problem was, so I'm going to assume that the character isn't changing color. You have a couple problems.
1. colorStart1 and colorEnd1 are both Color.blue. Given the Lerp formula is (1 - t) a + t b, if both "a" and "b" are blue, then any value in between them will also be blue. The same applies for colorStart2.

2.The next problem is related to the t in the Lerp formula. The "lerp" var will always = -1, so in Lerp it is clamped at 0. What it appears you are trying to do is have it fade from one color to another over 1 second. Then you might want to look at something like:

var hitPoints = 100.0; var deadReplacement : Transform; var dieSound : AudioClip; var colorStart1 = Color.blue; var colorEnd1 = Color.blue; var colorStart2 = Color.red; var colorEnd2 = Color.red; var playerMesh : Renderer; var duration = 1.0;

var col : Color; var changingColor = false;

function Start () { col = playerMesh.material.color; //cache the current color }

function ApplyDamage (damage : float) { if(!changingColor) {

     changingColor = true;
     hitPoints -= damage;

     if (hitPoints &lt;= 0.0)
         return;

     if (hitPoints &gt;= 51){   
         LerpColor(col, colorEnd1, duration);
     }

     else if (hitPoints &lt;= 50){   
         LerpColor(col, colorEnd2, duration);
     }


     if (hitPoints &lt;= 0.0)
     {
     }
 }

}

function LerpColor (start : Color, end: Color, time : float) { var i : float = 0; while(i <= 1) { col = Color.Lerp(start, end , i); playerMesh.material.color = col; i += Time.deltaTime / time; yield; } changingColor = false; }

This will change the objects color from the current color to the set color over the duration. The changingColor boolean should prevent LerpColor being called over and over while it is still running. This way, If you are still changing color, you cannot take anymore damage until you finish taking damage. I didn't test it though. Hope this helps some.

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Weapon Mesh Damage 1 Answer

player to kill ai 0 Answers

Enemy not taking damage on collisions. 2 Answers

Fade Screen Transition After Death 1 Answer

Do not pick item when item full 2 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