- Home /
simple color lerp...isn't lerping!
This should be pretty cut and dry. I'm still a beginner, but I've gotten translation lerps to work. But all this color lerp wants to do is stay on the color and nerver lerp back.
var normalColor : Color = Color.white;
var hitColor : Color = Color.red;
var hitFlash : float = 0.5f;
function FlashHit ()
{
renderer.material.color = hitColor;
renderer.material.color = Color.Lerp (hitColor, normalColor, hitFlash);
}
I'm expecting on execution of this function for the Object to instantly pop to hit color, then lerp back to normalcolor after .5 seconds. What I get is the object instantly flashes to the hit color, but never lerps back to normal color. Like the lerp isn't there.
Any ideas as to how I'm screwing this up?
Answer by ArkaneX · Sep 26, 2013 at 03:28 PM
I started writing an example, but then I noticed that vexe (who was also writing an example), found the nice post. At first I thought to discard my writings, but then I decided to post it as alternative, coroutine based, solution :)
function Update ()
{
if(Input.GetKeyDown(KeyCode.Space))
{
FlashHit();
}
}
function FlashHit ()
{
renderer.material.color = hitColor;
var t : float = 0;
while(t < 1)
{
t += Time.deltaTime / hitFlash;
renderer.material.color = Color.Lerp (hitColor, normalColor, t);
yield;
}
}
Haha that's pretty cool. Actually, there's another reason why I dropped my example (it's pretty much like yours, except I used t += dt / duration) is that for some reason the duration didn't seem to work for me, I set it to like 5 seconds, but it lerps instantly... Not sure what I missed.
I tried modifying your code to work within a boolean switch (isHit) but I'm still getting the same results. Enemy switches to red then stays red.
I'll post the full code here;
#pragma strict
var speed : float = 5.0f;
var strength : float = 1;
var hitPoints : int = 10;
var reloadTime : float = 1.5f;
var nextFireTime : float = 0.0f;
var hitFlash : float = 0.5f;
var attackSocket : Transform;
var projectile : GameObject;
var myTarget : Transform;
var normalColor : Color = Color.white;
var hitColor : Color = Color.red;
private var getClock : GameObject;
private var getPlayTime : float = 0.0f;
private var projectileStrength : int = 0;
private var currentSpeed : float = 0.0f;
private var getPlayTimeEnabled : boolean = true;
var isHit : boolean = false;
function Awake ()
{
var randomSpeed : float = Random.Range (0, .2); // creates a one time random value
speed += randomSpeed;
currentSpeed = speed;
renderer.material.color = normalColor;
}
function Update ()
{
getClock = GameObject.FindGameObjectWithTag("clock");
getPlayTime = getClock.GetComponent(clock).playTime;
getPlayTimeEnabled = getClock.GetComponent(clock).playTimeEnabled;
if ( getPlayTimeEnabled )
{
transform.Translate(Vector3.left * Time.deltaTime * speed ); // tells object to move left at speed
if (transform.position.x <= (-9.0))
{
Destroy (gameObject); // if object gets to end of screen, destroys it
}
if (hitPoints <= 0)
{
Destroy (gameObject);
}
if ( myTarget )
{
if ( getPlayTime >= nextFireTime )
{
FireProjectile ();
}
}
if ( myTarget == null )
{
speed = currentSpeed;
}
}
if (isHit)
{
FlashHit ();
}
}
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.tag == "bullet")
{
projectileStrength = other.GetComponent(tomatoProjectile).strength;
hitPoints -= projectileStrength;
projectileStrength = 0;
isHit = true;
}
if (other.gameObject.tag == "player")
{
speed = 0.0;
nextFireTime = getPlayTime + ( reloadTime * .5 );
myTarget = other.gameObject.transform;
}
}
function OnTriggerExit ( other : Collider )
{
if ( other.gameObject.transform == myTarget )
{
myTarget = null;
speed = currentSpeed;
}
}
function FireProjectile ()
{
nextFireTime = getPlayTime + reloadTime;
Instantiate(projectile, attackSocket.position, attackSocket.rotation);
}
function FlashHit ()
{
renderer.material.color = hitColor;
var t: float = 0.0f;
while (t < 1)
{
t += Time.deltaTime * hitFlash;
renderer.material.color = Color.Lerp (hitColor, normalColor, t);
yield;
isHit = false;
}
}
So the idea is to have OnTriggerEnter set the boolean to true, thus calling the function. Then the function shuts the switch off.
I tried to simulate your setup changing isHit inside OnTriggerEnter, but this still worked for me as expected. Could you please remove isHit = false;
from FlashHit, and insert it before calling FlashHit:
if (isHit)
{
isHit = false;
FlashHit ();
}
$$anonymous$$aybe this will help?
I also changed my code a bit - ins$$anonymous$$d of multiplying by hitFlash I now divide (@vexe - thanks for pointing it up!)
ugh...sometimes, I just wanna punch myself in the face! So I found the issue, although I tried divide and that didn't work. It wasn't until I simply added them together ( + ) that I got results. I'm not sure why that is.
But the issue was, and this is really, REALLY stupid of me...
Was that I had never updated the values (reset) them in the heirachy. So they still had hitFlash = 0.0; for some stupid reason. As soon as I gave it a value, It started lerping.
So thank you both for your help! I'm going to try divide to see if she will work with divide.
One final question I have that is related to this is; Do either of you know how to flash an object white? I'm not sure if it's possible because by default your textures are already the whitest value they could be, so I'm flashing them red for now. But if either of you know a light method to bloom out a texture without having to double up your texture count by creating white versions of anything you want to flash, I'm all ears!
Thanks again guys!