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

Controlling Scale of a GameObject?

Discussion in 'Scripting' started by blt3d, Nov 10, 2008.

  1. blt3d

    blt3d

    Joined:
    Oct 23, 2008
    Posts:
    192
    Any idea on how to control the scale of a gameobject? I'm looking for an effect similar to Jelly Car's car object. I have a script like this:
    Code (csharp):
    1.  
    2. var Scale : Vector3;
    3. function OnGUI()
    4. {
    5.    if(GUI.Button(Rect (375,250,100,50), "Grow"))
    6.    {
    7.       transform.localScale.x += 1;
    8.       transform.localScale.y += 1;
    9.       transform.localScale.z += 1;
    10.    }
    11.    else
    12.    {
    13.       transform.localScale.x += 0;
    14.       transform.localScale.y += 0;
    15.       transform.localScale.z += 0;
    16.    }
    17. }    
    I want the GUI Button to grow the object in scale, but shrink back to the original size after alittle while once the button is let go. Any ideas or help would be greatly appreciated!
     
  2. p-lux

    p-lux

    Joined:
    Oct 21, 2008
    Posts:
    59
    Hi,

    you could :

    - keep the original scale in a class variable
    - use a Lerp to change the scale of the cube
    - use a Timer (System.Timers.Timer in C#) to wait for a moment
    - Lerp again to re-scale the cube to its original scale

    In JavaScript, I don't know...maybe a SetTimeout ???
     
  3. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Easiest way is to make a function called "Shrink" that runs when Input.GetMouseButtonUp(0) == true. It does this:

    Code (csharp):
    1. yield WaitForSeconds(1);
    2. while (transform.localScale.x > holder.x) {
    3.    transform.localScale.x -= 1;
    4.    transform.localScale.y -= 1;
    5.    transform.localScale.z -= 1;
    6.    yield;
    7. }
    Oh, you don't need the else statement in your button, by the way. There's no point to telling it to grow by nothing if it isn't clicked.
     
  4. blt3d

    blt3d

    Joined:
    Oct 23, 2008
    Posts:
    192
    I'm getting an error that OnGUI() cannot be a coroutine when I try to use Yield. Is there a different way to determine when the user lets go of a GUI button? I'm using Unity iPhone, so OnMouseButtonUp doesn't work :(
     
  5. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Well, don't put it in OnGUI, then. Have OnGUI start a function that runs the code when the button isn't being pressed.

    Code (csharp):
    1. function Shrink()
    2. {
    3.    if (!started) {
    4.       started = true;
    5.       yield WaitForSeconds(1);
    6.       while (transform.localScale.x > holder.x  started) {
    7.          transform.localScale.x -= 1;
    8.          transform.localScale.y -= 1;
    9.          transform.localScale.z -= 1;
    10.          yield;
    11.       }
    12.    }
    13.    started = false;
    14. }
    Add this line to your button-press:
    started = false;

    Add this variable to your script:
    var started : boolean = false;

    Add these lines to your else and dump the old lines:
    if (!started) {
    Shrink();
    }
     
  6. blt3d

    blt3d

    Joined:
    Oct 23, 2008
    Posts:
    192
    Thanks alot for the help! Works great now :)