Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  6. Unity 2017.2 beta is now available for download.
    Dismiss Notice

Slowly scale object

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

  1. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    377
    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:
    377
  4. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    377
    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,423
    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:
    377
    FFFUUUUUUU!!!
    thnx for that carking1996... Should have think that out myself.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,303
    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:
    114
    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. }