Search Unity

Resolved Slightly Increase GameObject Scale While GameObject Is Constantly Shrinking

Discussion in 'Scripting' started by cvionis, Jan 15, 2021.

  1. cvionis

    cvionis

    Joined:
    Dec 23, 2020
    Posts:
    6
    Hello,

    I'm currently using the following script to gradually shrink the scale of the player (a sphere) in my game:

    Code (CSharp):
    1.   // Time it takes in seconds to shrink from starting scale to target scale.
    2.   public float ShrinkDuration = 5f;
    3.  
    4.     // The target scale
    5.     public Vector3 TargetScale = Vector3.one * .5f;
    6.  
    7.     // The starting scale
    8.     Vector3 startScale;
    9.  
    10.     // T is our interpolant for our linear interpolation.
    11.     float t = 0;
    12.  
    13.     void OnEnable()
    14.     {
    15.         startScale = transform.localScale;
    16.  
    17.         t = 0;
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         t += Time.deltaTime / ShrinkDuration;
    23.  
    24.         Vector3 newScale = Vector3.Lerp(startScale, TargetScale, t);
    25.         transform.localScale = newScale;
    26.  
    27.         if (t > 1)
    28.         {
    29.             enabled = false;
    30.         }
    31.     }
    However, I also need to increase the scale of the player by a small amount every time it enters a certain trigger. Here is the code I currently have for doing so:

    Code (CSharp):
    1. private void OnTriggerEnter(Collider trig)
    2.     {
    3.         GameObject.Find("PlayerBody").transform.localScale += new Vector3(2, 2, 2);
    4.     }
    The problem I am having is that the script making the player grow only works when the script making the player gradually shrink is removed from the player or disabled. How would I make the player gradually shrink but also make the scale of the player increase slightly when it enters a certain trigger?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    I would keep a separate float that starts at 1.0 and goes down periodically, and then when you hit your trigger, increase it.

    Code (csharp):
    1. private float MyScaleFloat = 1.0f;
    Then use that float every frame to set the size:

    Code (csharp):
    1. playerTransform.localScale = Vector3.one * MyScaleFloat;
    EDIT: looking at your code above, the shrinking code is producing fresh scales from your min to max scale. This is why growing doesn't work: you grown the .localScale and then it gets wiped out by the assignment from Lerp next frame.

    I stand by my recommendation above: nothing in your code should do anything to scale, except that one assignment in my sample code. Everything else would hit the scale variable.
     
    Last edited: Jan 15, 2021
  3. cvionis

    cvionis

    Joined:
    Dec 23, 2020
    Posts:
    6
    @Kurt-Dekker thanks for the help! I was eventually able to create a script using your suggestions to make the scaling work properly.
     
    Kurt-Dekker likes this.