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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to stop 2 or more gameObjects from continuing the same lerp of color?

Discussion in 'Scripting' started by AlmantusK, Oct 7, 2015.

  1. AlmantusK

    AlmantusK

    Joined:
    Oct 7, 2015
    Posts:
    30
    Hello again. So I have this script, that gets all children of a game object which got hit by a ray. Then it uses a script attached to each of those children to set the color of material by having begin and end colors preassigned. Everything works, except that lerp for children of different parent continues the lerp of previous parent and their children.
    Here is the code that get all children:

    Code (CSharp):
    1. //-------
    2. toDestroy= hit.transform.gameObject;
    3.                 if(toDestroy.tag == "Destructible")//if ray hits object with this tag
    4.                 {
    5.                     if (time== 1.5f)//it delays 1.5s to give time for lerp
    6.                         children = toDestroy.transform.childCount;//how many children
    7.                     for (int i = 0; i < children-1; i++)
    8.                         toDestroy.transform.GetChild(i).GetComponent<DynamicColorChange>().Change ();//for each child lerp their color
    9.                     time = time - Time.deltaTime;
    10.                     if (time <= 0)
    11.                         toDestroy.GetComponent<Destructible>().boom();// when 1.5s+ passed, destroy the replace the object
    12.                 }
    13.                 else
    14.                     time = 1.5f;// timer set to default value
    15. //-------
    And here's the code that lerps the color:
    Code (CSharp):
    1.  
    2.  
    3. public class DynamicColorChange : MonoBehaviour {
    4.     private Color colorStart = Color.red;
    5.     public Color colorEnd = Color.green;
    6.     float duration = 2.0f;
    7.     public Renderer rend;
    8.     int k = 0;
    9.     void Start() {
    10.         rend = GetComponent<Renderer>();
    11.     }
    12.  
    13.     public void Change()
    14.     {
    15.         float lerp = Mathf.PingPong(Time.time, duration) / duration;
    16.         rend.material.color = Color.Lerp(colorStart, colorEnd, lerp);
    17.         //colorStart = colorEnd;
    18.     }
    19.  
    20.  
    Presume that objects 1,2,3,4,5 were hit by a ray the same ammount of time one after another. It's as first object pauses his lerp and second one continues the first one's lerp, third one does the same with second and so on..
    In the attached file there's the illustration of what I mean.

    So please help me fix it so that all children of same parent would restart the whole change in colors, but not continue it. (It's for freeze-and-shatter effect using a laser).

    P.S. I am aware that I don't do a full lerp before object gets destroyed (duration>time) but it's ok and not an issue (I just want the lerp not to repeat before you destroy an object).
     

    Attached Files:

  2. AlmantusK

    AlmantusK

    Joined:
    Oct 7, 2015
    Posts:
    30
    Also, if I am correct, the material should start from colorStart and become endColor, but instead it goes from default color of material to endColor and then from endColor to startColor. So how to fix it so that it goes to only one color?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're basing your lerp off the current time of the game, when you hit the second object the time is the same as the time for the first object....

    if you want the lerp to start again from the beginning you will need to store the current time at the point when the effect starts and deduct that from the current time for the duration of the lerp
     
  4. AlmantusK

    AlmantusK

    Joined:
    Oct 7, 2015
    Posts:
    30
    So basically duration= duration - time.deltatime? inside the object check script and then pass the duration value to the script or can i check time driectly inside the child script? I just realised that I can add time.delta time in my update function within the child script, so I guess it might work.
    p.s. Thanks for the tip:)
     
    Last edited: Oct 7, 2015
  5. AlmantusK

    AlmantusK

    Joined:
    Oct 7, 2015
    Posts:
    30
    Tried this but now it lerps very fast one object only and when ray hits other object it instantly becomes endColor. I don't undertand why in the Start() the duration for lerping doesn't become 2s, but 0. Any way how to do it from within child script (this one)?
    Code (CSharp):
    1.  
    2.  
    3. void Start() {
    4.         rend = GetComponent<Renderer>();
    5.         duration = 2.0f;
    6.     }
    7.     void Update() {
    8.         while(duration>0)
    9.           duration = duration - Time.deltaTime;
    10.     }
    11.  
    12.     public void Change()
    13.     {
    14.         float lerp = Mathf.PingPong(Time.time, duration) / duration;
    15.         rend.material.color = Color.Lerp(colorStart, colorEnd, lerp);
    16.         //colorStart = colorEnd;
    17.     }
    18.