How To make an object flicker between alpha values?

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.

1 Like

did you use a coroutine? or did you try to use this inside the oncollision() function?

if you want the effect to happen over time you’ll need to call a coroutine. oncollision is only called for the single frame the collision happens.

I’m calling it inside OnCollision method. This is how i’m calling it

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();
}

}
}

PlayerOnCollision function handles the alpha change.

What do you suggest I should do?

You’ve omitted the part that would contain the problem. Can you post PlayerOnCollision?

Also, use [co de] [/co de] tags.

// 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; }

Here’s my code for PlayerOnCollision

void PlayerOnCollision()
{
BroadcastMessage("MakeTransparent", 0.3f, SendMessageOptions.DontRequireReceiver);
}

Coz the playerScript, I mean the above script is attached to a parent object which doesn’t have a renderer component.

this message is received by the rendererScript attached to the children with the renderer component attached and goes like this

private Material currentMaterial;
	public Material transparentMaterial;
	private Texture currentTexture;

	void Start()
	{
		currentMaterial = gameObject.renderer.material;
		currentTexture = gameObject.renderer.material.mainTexture;
	}

	void MakeTransparent(float alphaTransparent)
	{
		gameObject.renderer.material = transparentMaterial;
		gameObject.renderer.material.color = new Color(renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, alphaTransparent);
		gameObject.renderer.material.SetTexture("_MainTex",currentTexture);
	}

	void MakeVisible(float alphaVisible)
	{
		gameObject.renderer.material = currentMaterial;
		gameObject.renderer.material.color = new Color(renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, alphaVisible);
	}

Hope this helps in finding the solution.