Hey Guys! I’m assigning a transparent material(Custom Material) to my character on collision with 0.3 alpha value. I want to if there is way I can make it flicker between alpha =0.3f and alpha =1.0f . I’ve tried using Mathf.PingPong, but it didn’t work for me. I’ve tried many things but not able to achieve the effect i want.
I want the flickering effect as you guys might have seen in many video games when a player gets hit by anything. Please Help Me. Thank You.
// I have not checked your OnCollisionEnter code.
// I assume it works.
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "Vehicle") {
if (collision.collider.GetType()== typeof(CapsuleCollider) playerTotalHealth >= 10) {
int reducedPlayerHealth = playerTotalHealth - amountOfHealthLossOnCollision;
playerTotalHealth = reducedPlayerHealth;
playerHealth.guiText.text = reducedPlayerHealth.ToString();
PlayerOnCollision();
}
}
}
void PlayerOnCollision() {
// flicker for one second into the future
flickerEndTime = Time.time + 1f;
}
void Update() {
if (Time.time < flickerEndTime) {
Color flashAlpha = renderer.material.color;
// PingPong between 0.3 and 1;
flashAlpha.a = 0.3f + Mathf.PingPong(Time.time, 0.7f);
// Assuming your renderer is on this.gameObject!
renderer.material.color = flashAlpha;
}
else {
renderer.material.color = Color.white;
}
}
/// If this is in the future, we are flickering.
float flickerEndTime { get; set; }