Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How To make an object flicker between alpha values?

Discussion in 'Scripting' started by Amaresh, May 12, 2014.

  1. Amaresh

    Amaresh

    Joined:
    Jan 22, 2014
    Posts:
    21
    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.
     
    Krish-Vikram likes this.
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.
     
  3. Amaresh

    Amaresh

    Joined:
    Jan 22, 2014
    Posts:
    21
    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?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    You've omitted the part that would contain the problem. Can you post PlayerOnCollision?

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

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Code (csharp):
    1.  
    2. // I have not checked your OnCollisionEnter code.
    3. // I assume it works.
    4. void OnCollisionEnter(Collision collision) {
    5.   if (collision.gameObject.tag == "Vehicle") {
    6.     if (collision.collider.GetType()== typeof(CapsuleCollider)  playerTotalHealth >= 10) {
    7.       int reducedPlayerHealth = playerTotalHealth - amountOfHealthLossOnCollision;
    8.       playerTotalHealth = reducedPlayerHealth;
    9.       playerHealth.guiText.text = reducedPlayerHealth.ToString();
    10.       PlayerOnCollision();
    11.     }
    12.   }
    13. }
    14.  
    15. void PlayerOnCollision() {
    16.   // flicker for one second into the future
    17.   flickerEndTime = Time.time + 1f;
    18. }
    19.  
    20. void Update() {
    21.   if (Time.time < flickerEndTime) {
    22.     Color flashAlpha = renderer.material.color;
    23.     // PingPong between 0.3 and 1;
    24.     flashAlpha.a = 0.3f + Mathf.PingPong(Time.time, 0.7f);
    25.     // Assuming your renderer is on this.gameObject!
    26.     renderer.material.color = flashAlpha;
    27.   }
    28.   else {
    29.     renderer.material.color = Color.white;
    30.   }
    31. }
    32.  
    33. /// If this is in the future, we are flickering.
    34. float flickerEndTime { get; set; }
    35.  
     
    Last edited: May 13, 2014
  6. Amaresh

    Amaresh

    Joined:
    Jan 22, 2014
    Posts:
    21
    Here's my code for PlayerOnCollision
    Code (csharp):
    1.  
    2. void PlayerOnCollision()
    3. {
    4. BroadcastMessage("MakeTransparent", 0.3f, SendMessageOptions.DontRequireReceiver);
    5. }
    6.  
    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

    Code (csharp):
    1.  
    2.  
    3. private Material currentMaterial;
    4.     public Material transparentMaterial;
    5.     private Texture currentTexture;
    6.  
    7.     void Start()
    8.     {
    9.         currentMaterial = gameObject.renderer.material;
    10.         currentTexture = gameObject.renderer.material.mainTexture;
    11.     }
    12.  
    13.     void MakeTransparent(float alphaTransparent)
    14.     {
    15.         gameObject.renderer.material = transparentMaterial;
    16.         gameObject.renderer.material.color = new Color(renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, alphaTransparent);
    17.         gameObject.renderer.material.SetTexture("_MainTex",currentTexture);
    18.     }
    19.  
    20.     void MakeVisible(float alphaVisible)
    21.     {
    22.         gameObject.renderer.material = currentMaterial;
    23.         gameObject.renderer.material.color = new Color(renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, alphaVisible);
    24.     }
    25.  
    26.  
    Hope this helps in finding the solution.