Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Scale an object up and down between max and min value smooth

Discussion in 'Scripting' started by aoleon, May 27, 2010.

  1. aoleon

    aoleon

    Joined:
    May 26, 2010
    Posts:
    22
    How do you scale an object repeatedly between a max and min scale value over a time interval smoothly? (as in no jumps or jitters).

    I am new to both Unity and scripting.
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Here, this should get you started.

    Code (csharp):
    1. var amplitude : float;
    2.  
    3. var scale : Vector3;
    4. function Update ()
    5. {
    6.     for (i = 0; i < 3; ++i) scale[i] = amplitude * Mathf.Sin(Time.time) + 1;
    7.     transform.localScale = scale;
    8. }
     
    peterroth124 and TankThunderbird like this.
  3. aoleon

    aoleon

    Joined:
    May 26, 2010
    Posts:
    22
    Very cool!

    I will try that and let you know.

    Thanks so much!
     
  4. aoleon

    aoleon

    Joined:
    May 26, 2010
    Posts:
    22
    Ok thanks!
     
  5. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    It means look at my post again. I'm fixing it. :)
     
  6. aoleon

    aoleon

    Joined:
    May 26, 2010
    Posts:
    22
    I appreciate you fixing it.

    Can you tell me what that error means and why it occured... so I can learn something about scripting?

    Thanks!
     
  7. aoleon

    aoleon

    Joined:
    May 26, 2010
    Posts:
    22
    That worked!

    Is there a way I can change the amplitude with a max and min variable? I don't want it going to 0 size.

    Thanks again!
     
  8. aoleon

    aoleon

    Joined:
    May 26, 2010
    Posts:
    22
    Here is the final version that i tweaked successfully to have max and min.

    Code (csharp):
    1. var maxAmp : float;
    2. var minAmp : float;
    3.  
    4. var scale : Vector3;
    5. function Update ()
    6. {
    7.    for (i = 0; i < 3; ++i) scale[i] = maxAmp + minAmp * Mathf.Sin(Time.time) + 1;
    8.    transform.localScale = scale;
    9. }
    Can you please explain what the function is doing? What is the i variable? where it says i=0; i < 3; ++i) .... what is that all about?
     
  9. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Sure. Transform.localScale is a Vector3, which is three floats grouped together. I tried to assign a float to it, like this:
    Code (csharp):
    1. transform.localScale = amplitude * Mathf.Sin(Time.time) + 1;
    I know that's very wrong. However, I try to never use non-uniform scaling, because it can cause problems when parenting and rotation are combined. Therefore, localScale, as far as I'm concerned, is a float. I think that's why my brain shut down that piece of my knowledge temporarily. :roll:

    It's cool that you already started tweaking that to your liking. Here's a version with more control. See if it's to your liking. It doesn't use a max and min; instead, you define a "base" scale, and the ratio parameter makes it either that many times bigger or smaller at the max change – it will never hit zero, and the scale change is rationally symmetrical.
    Code (csharp):
    1. var rate : float;
    2. var midScale : float;
    3. var ratio : float;
    4.  
    5. // Drag this game object onto this variable slot.
    6. // It makes the code run faster than using "transform" by itself.
    7. var thisTransform : Transform;
    8.  
    9. private var scale : Vector3;
    10. function Update ()
    11. {
    12.     var scaleComponent = midScale * Mathf.Pow(ratio, Mathf.Sin(Time.time * rate));
    13.     for (i = 0; i < 3; ++i) scale[i] = scaleComponent;
    14.     thisTransform.localScale = scale;
    15. }
    Well, that's a for loop, a very basic element of all programming. I won't define that for you, because it's super easy to look up, and has surely been explained better elsewhere. But the reason we need to use a loop is due to that localScale being a Vector3 issue. Once you know what a for loop does, You can see in this page of the manual, that assigning the same thing, using that loop, is an efficient way to change the uniform scale of an object.
     
  10. KarthiSucc9

    KarthiSucc9

    Joined:
    Nov 30, 2015
    Posts:
    5
    It is not working in C#. I am new bie. Could any one can give it in C#...
     
  11. bigSky

    bigSky

    Joined:
    Jan 31, 2011
    Posts:
    114
    Here's the conversion...some fun learning things added:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Siny : MonoBehaviour {
    5.  
    6.     float rate;
    7.     float midScale;
    8.     float ratio;
    9.  
    10.     // Drag this game object onto this variable slot.
    11.     // It makes the code run faster than using "transform" by itself.
    12.     public Transform thisTransform;
    13.  
    14.     //alternatively, if it is the owning object that gets the scale changes, you can get it automatically, on Start
    15.     //this checks to see if it the variable thisTransform is assigned, and if it isn't, assigns it to the current transform.
    16.     void Start(){
    17.         if(thisTransform==null){
    18.         thisTransform=transform;
    19.         }
    20.     }
    21.  
    22.     private Vector3 scale;
    23.     void Update ()
    24.     {
    25.         float scaleComponent = midScale * Mathf.Pow(ratio, Mathf.Sin(Time.time * rate));
    26.         for (int i = 0; i < 3; i++) {
    27.             scale = scaleComponent*Vector3.one;
    28.             thisTransform.localScale = scale;
    29.         }
    30.     }
    31.  
    32.         //Alternatively, you could use Mathf.PingPong...
    33.  
    34.  
    35.         void PingPong(){
    36.             thisTransform.localScale=Mathf.PingPong(Time.time*rate,ratio)*Vector3.one;
    37.         }
    38.     }
    39.  
     
  12. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    I once read somewhere why not to use Time.time for these kinds of calculations in a Update.
    It was something like as you keep playing the game.. the calculation overhead increases since the value of Time.time gets bigger.