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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't compare transform.localScale with a float?

Discussion in 'Scripting' started by LegendXV, Jun 3, 2015.

  1. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    Transform.localScale returns a float integer, so how come it gives me an error in this script?

    Code (csharp):
    1.  
    2. void Update () {
    3.         print (this.transform.localScale);
    4.         if (shrinking) {
    5.             this.transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (targetScale, targetScale, targetScale), Time.deltaTime * shrinkSpeed);
    6.             if (this.transform.localScale < targetScale) {
    7.                 shrinking = !shrinking;
    8.             }
    9.         }
    10.     }
    11.  
    Something about being unable to compare Vector3 to a float, but localScale returns a float...
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    localScale returns a Vector3. Has done for as far back as I can remember.
     
  3. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    Oh, I accessed Vector3 with ".x" on the end. Thanks!


    Code (csharp):
    1.  
    2. void Update () {
    3.         if (shrinking) {
    4.             print ("X " + this.transform.localScale.x);
    5.             this.transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (targetScale, targetScale, targetScale), Time.deltaTime * shrinkSpeed);
    6.             if (this.transform.localScale.x < targetScale) {
    7.                 print ("Done!");
    8.                 shrinking = false;
    9.             }
    10.         }
    11.     }
    I'm still having problems here. It won't stop shrinking when the localScale is less than the target scale. I even printed out the .x value to see if it was a float and it is.

    I have the targetScale set at .6f, and when transform.localScale.x goes below .6 it doesn't stop shrinking. However, if I swap the < to > in the comparison, it works. So I'm not understanding why it can't detect the < comparison.
     
  4. NeilM0

    NeilM0

    Joined:
    Mar 31, 2009
    Posts:
    135
    The lerp will shrink the local scale to the targetScale, but it won't be less than it. If you change the if statement to read <= instead of just < I wouldn't be surprised if it worked. You may also want to look into Mathf.Approximately and Vector3.Distance. One will tell you the distance between two vector3's, and the other will tell you when a float is approximately equal to another float.
     
  5. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    I'm not quite sure how it operates. Here's my variable ...

    targetScale = .6f;

    That means I want to shrink it until it's .6 the original size? Either way, when I print transform.localScale.x it prints out some low number like .32 when the size I wanted it to stop at was .6.
     
  6. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    Vector 3 tran = transform.localScale;

    float x = tran.x;
     
  7. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    Thanks, I got it, but forgot to reply that I had gotten it. I'm curious though, how do I slow the rate of shrinking? No matter what value I change it doesn't seem to slow it down.
     
  8. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    Bump*
     
  9. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    simple since ur lerp is doing inside the if statement, therefore, it won't work
     
  10. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    It doesn't work even if I take it out.

    Code (csharp):
    1.  
    2. private float targetScale = .2f;
    3. private float shrinkSpeed = .01f;
    4.  
    5.     void Update () {
    6.         if (go) {
    7.             Vector3 pos = go.transform.position;
    8.             pos.x = this.transform.position.x;
    9.             pos.z = this.transform.position.z;
    10.             go.transform.position = pos;
    11.         }
    12.  
    13.         if (targetScale >= this.transform.localScale.x && shrinking) {
    14.             print ("Done!");
    15.             shrinking = false;
    16.         }
    17.          
    18.         if (shrinking) {
    19.             print ("Shrinking!");
    20.             this.transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (targetScale, targetScale, targetScale), Time.deltaTime * shrinkSpeed);
    21.         }
    22.             //print ("X " + this.transform.localScale.x);
    23.     }
    They all still shrink at the same speed, and never stop shrinking.
     
  11. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    quick code for here
    Code (CSharp):
    1.     Vector3 newScale;
    2.     void Start()
    3.     {
    4.         newScale = this.transform.localScale;
    5.     }
    6.  
    7.     void Update()
    8.     {
    9.         if(Input.GetKeyDown(KeyCode.A))
    10.         {
    11.             newScale = new Vector3(1.5f, 1.5f, 1.5f);
    12.         }
    13.         if(Input.GetKeyDown(KeyCode.B))
    14.         {
    15.             newScale = new Vector3(1.0f, 1.0f, 1.0f);
    16.         }
    17.      
    18.         this.transform.localScale = Vector3.Lerp(this.transform.localScale, newScale, 10 * Time.deltaTime)
    19.     }
     
    Last edited: Jun 3, 2015
  12. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    I want to shrink it, though. Wouldn't 1.5 make it grow, or is that not how it works?
     
  13. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    this is just a sample, drag the scripts to a cube object and you will understand how lerp work
    just show you a way only.
    my Lerp is not doing inside the if statement.
    But your lerp are

    1.0 is default value, 1.5 means scale large
     
    Last edited: Jun 3, 2015
  14. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    Still not working. Weird thing is I set shrinking to false, but it continues to execute shrinking. If you could, please try running it.

    Edit: Got it working with this.

    Code (csharp):
    1.  
    2. if (shrinking) {
    3.             this.transform.localScale -= Vector3.one * Time.deltaTime * shrinkSpeed;
    4.             print ("Shrinking!" + this.transform.localScale.x);
    5.             if (this.transform.localScale.x < targetScale) {
    6.                 print ("Done!");
    7.                 shrinking = false;
    8.             }
    9.         }

    Code (csharp):
    1.  
    2. private Vector3 newScale;  
    3.  
    4. void Update () {
    5.         if (go) {
    6.             Vector3 pos = go.transform.position;
    7.             pos.x = this.transform.position.x;
    8.             pos.z = this.transform.position.z;
    9.             go.transform.position = pos;
    10.         }
    11.  
    12.         if (targetScale >= this.transform.position.x && shrinking) {
    13.             print ("Done: " + this.transform.position.x);
    14.             shrinking = false;
    15.         }
    16.  
    17.         if (shrinking) {
    18.             print ("Shrinking!");
    19.             print ("Shrinking: " + this.transform.position.x);
    20.             this.transform.localScale = Vector3.Lerp (this.transform.localScale, newScale, Time.deltaTime * shrinkSpeed);
    21.         }
    22.             //print ("X " + this.transform.localScale.x);
    23.     }
    24.  
     
    Last edited: Jun 3, 2015