Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Ping pong not working

Discussion in 'Scripting' started by michellynitecki, Jul 10, 2019.

  1. michellynitecki

    michellynitecki

    Joined:
    Apr 26, 2019
    Posts:
    26
    I'm trying to insert a ping pong lerp between one color and another but nothing happens, what's missing
    Code (CSharp):
    1.     public Color colorOne;
    2.     public Color Alpha;
    3.  
    4.     void Update(){
    5.         Alpha = Color.Lerp(colorOne, Alpha, Mathf.PingPong(Time.time, 1));
    6.     }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,358
    You're taking a lerp between colorOne and Alpha and assigning it to Alpha. This will immediately set Alpha to the same value as colorOne (since Time.time will be near 0 the first time it runs), at which point it will be pingponging between two copies of the same color.

    Assign the result of the lerp to a third variable instead of using one of the two you're feeding into it.
     
  3. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    192
    try :
    Code (CSharp):
    1. public Color colorOne;
    2. public Color colorTwo;
    3. public Color Alpha;
    4.     void Update(){
    5.         Alpha = Color.Lerp(colorOne, colorTwo, Mathf.PingPong(Time.time, 1));
    6.     }
     
  4. michellynitecki

    michellynitecki

    Joined:
    Apr 26, 2019
    Posts:
    26
    It's very strange this, he does ping pong in the inspector, but he is not attacking the sprite.

    Code (CSharp):
    1.     public List<SpriteRenderer> list;
    2.     public Color colorOne,color;
    3.     public Color Alpha;
    4.  
    5.  
    6.     void Update()
    7.     {
    8.         for (int i = 0; i < List.Count; i++)
    9.         {
    10.             list[i].GetComponent<SpriteRenderer>().color = colorOne;
    11.             colorOne = Color.Lerp(Alpha, color, Mathf.PingPong(Time.time, 1));
    12.         }
    13.     }
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,358
    The GetComponent call on line 10 is unnecessary (You already have the SpriteRenderer). You should also move line 11 before the for loop, as that result won't change as you go through the loop.
    All that said, that code ought to work. Have you linked the appropriate sprite object in your "list" in the inspector?
     
  6. michellynitecki

    michellynitecki

    Joined:
    Apr 26, 2019
    Posts:
    26
    very good, thank you, but one thing left me worried, I use many (for) and keep updating several. This is bad, could this (Crash) in Unity?

    Code (CSharp):
    1.     void Update(){
    2.         if(index == 0)
    3.         {
    4.             for (int i = 0; i < List.Count; i++)
    5.             {
    6.                 corOne = Color.Lerp(Alpha, color, Mathf.PingPong(Time.time, speed));
    7.                 List[i].color = corOne;
    8.             }
    9.         }
    10.         else if (index == 1)
    11.         {
    12.             for (int i = 0; i < List.Count; i++)
    13.             {
    14.                 corOTwo = Color.Lerp(Alpha, color, Mathf.PingPong(Time.time, speed));
    15.                 List[i].color = corOTwo;
    16.             }
    17.         }
    18.         else if (index == 2)
    19.         {
    20.             for (int i = 0; i < List.Count; i++)
    21.             {
    22.                 corOThree = Color.Lerp(Alpha, color, Mathf.PingPong(Time.time, speed));
    23.                 List[i].color = corOThree;
    24.             }
    25.         }
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,358
    It's not going to crash Unity by a long shot, no. You'd have to work much harder to crash Unity. :)
     
    karl_jones and SparrowsNest like this.
  8. michellynitecki

    michellynitecki

    Joined:
    Apr 26, 2019
    Posts:
    26
    Thank you for your help:)