Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Slowly scale object

Discussion in 'Scripting' started by hellcaller, Jun 2, 2011.

  1. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    381
    Hi, i've got a little noob question. How can i slowly scale object's x and y from 1 to, for example, 5 during certain period of time?
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    This should scale an object's x and y from 1 to 5 over 10 seconds when applied to that object.

    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.     var newScale : float = Mathf.Lerp(1, 5, Time.deltaTime / 10);
    5.     transform.localScale = Vector3(newScale, newScale, 1);
    6. }
    7.  
     
  3. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    381
    Code (csharp):
    1. function Update()
    2. {
    3.     var newScale : float = Mathf.Lerp(1, 5, Time.time);
    4.     transform.localScale = Vector3(newScale, 1, newScale);
    5. }
    works perfect
     
  4. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    381
    Well... not that perfect(( I used this script on a blood splatter sprite which is instantiated when the player is killed. But sprite is starting to scale just at the moment when the game is started, and not when it is instantiated...
     
  5. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,608
    Code (csharp):
    1. function Update()
    2. {
    3.  
    4. if (do something here) {
    5.     var newScale : float = Mathf.Lerp(1, 5, Time.time);
    6.     transform.localScale = Vector3(newScale, 1, newScale);
    7. }
    8.  
    9. }
     
  6. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    381
    FFFUUUUUUU!!!
    thnx for that carking1996... Should have think that out myself.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. var finalScale = 5.0;
    2. var scaleSpeed = 2.0;
    3.  
    4. function Start () {
    5.     var timer = 0.0;
    6.     while (timer <= 1.0) {
    7.         timer += Time.deltaTime * scaleSpeed;
    8.         var newScale = Mathf.Lerp(1.0, finalScale, timer);
    9.         transform.localScale = Vector3(newScale, newScale, 1.0);
    10.         yield;
    11.     }
    12. }
     
  8. Rispat-Momit

    Rispat-Momit

    Joined:
    Feb 14, 2013
    Posts:
    265
    Here is a combination of the above:

    Code (JavaScript):
    1. var SeedHeight : float;
    2. var FinalHeigh : float;
    3. var Speed : float;
    4.  
    5. function Update()
    6. {
    7.     var newScale : float = Mathf.Lerp(SeedHeight, FinalHeigh, Time.time*Speed);
    8.     transform.localScale = Vector3(newScale, newScale, newScale);
    9. }