Search Unity

Scale inside coroutine

Discussion in 'Scripting' started by pzy2017, Jun 18, 2019.

  1. pzy2017

    pzy2017

    Joined:
    May 9, 2019
    Posts:
    8
    Hi,

    I have a project in which the user is allowed to change the rotation and scaling of an object. When the object is "released", a coroutine should smoothly reset the object to it's original scale and rotation. The problem I am having is that the rotation reset works, but not the scaling.

    Does anybody know why that happens?

    Thanks,
    Paul

    This is my code:

    Code (CSharp):
    1. float counter = 0f;
    2.  
    3. while (counter < 1)
    4. {
    5.  
    6.     currentProduct.transform.localScale = Vector3.Lerp(sca, originalScale, counter);
    7.     rotateTransform.transform.localRotation = Quaternion.Slerp(rot2, originalRotationValue, counter);
    8.     currentProduct.transform.localRotation = Quaternion.Slerp(rot1, originalRotationValue, counter);
    9.  
    10.     currentProduct.position = Vector3.Lerp(pos, originalPosition, counter);
    11.  
    12.     counter += Time.deltaTime / state0duration;
    13.     yield return null;
    14.  
    15. }
     
    Last edited: Jun 18, 2019
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Check the values that are applied to the localScale.

    E.g. breakpoint @ 6 / print those sca and originalScale. Maybe they're not what you're expecting.
    Also, make sure that scaling is not changed somewhere else.
     
  3. SplashFoxGames

    SplashFoxGames

    Joined:
    Oct 10, 2012
    Posts:
    53
    didn't test this code, but this is what I used:

    Code (CSharp):
    1. float elapsedTime = 0;
    2. Vector3 startScale = new Vector3(0f,0f,0f);
    3. Vector3 targetScale = new Vector3(1f,1f,1f);
    4.  
    5. card.transform.localScale = startScale;
    6.  
    7. float timeTakes = 1f    // animation will take one second
    8.  
    9. while (elapsedTime < timeTakes)
    10. {
    11.     card.localScale = Vector3.Lerp(card.transform.localScale, targetScale, (elapsedTime / timeTakes));
    12.  
    13.     elapsedTime += Time.deltaTime;
    14.     yield return new WaitForEndOfFrame();
    15. }