Unity Community


Results 1 to 6 of 6

  1. Location
    Lake Mary, FL
    Posts
    192

    Controlling Scale of a GameObject?

    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:  
    1. var Scale : Vector3;
    2. function OnGUI()
    3. {
    4.    if(GUI.Button(Rect (375,250,100,50), "Grow"))
    5.    {
    6.       transform.localScale.x += 1;
    7.       transform.localScale.y += 1;
    8.       transform.localScale.z += 1;
    9.    }
    10.    else
    11.    {
    12.       transform.localScale.x += 0;
    13.       transform.localScale.y += 0;
    14.       transform.localScale.z += 0;
    15.    }
    16. }

    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. 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. Location
    Missouri
    Posts
    1,529
    Easiest way is to make a function called "Shrink" that runs when Input.GetMouseButtonUp(0) == true. It does this:

    Code:  
    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.
    Available for consulting:
    http://forum.unity3d.com/threads/960...Personal-Tutor
    http://www.unityprogrammer.com

    Just released The Farming Game: Software Edition, a remake of a popular board game. Check it out at the link below!
    http://thefarminggame.net/?ref=Pride

    For tutorials on how to use Unity go to my website at:
    http://www.unity-tutorials.com


  4. Location
    Lake Mary, FL
    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. Location
    Missouri
    Posts
    1,529
    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:  
    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();
    }
    Available for consulting:
    http://forum.unity3d.com/threads/960...Personal-Tutor
    http://www.unityprogrammer.com

    Just released The Farming Game: Software Edition, a remake of a popular board game. Check it out at the link below!
    http://thefarminggame.net/?ref=Pride

    For tutorials on how to use Unity go to my website at:
    http://www.unity-tutorials.com


  6. Location
    Lake Mary, FL
    Posts
    192
    Thanks alot for the help! Works great now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •