0
\$\begingroup\$

I am using the following code to shrink a sprite:

public bool shrinking = false;
public float targetScale = 0.1f;
public float shrinkSpeed = 0.1f;

void Update () {
    if(shrinking) {
        mySprite.transform.localScale -= Vector3.one * Time.deltaTime * shrinkSpeed;
        if(mySprite.transform.localScale.x <= targetScale) {
            shrinking = false;
        }
    }
}

However, no matter what I change shrinkSpeed to, the object shrinks at the same rate. I know it probably has something to do with putting it in the update function but where should I move it to fix the problem?

\$\endgroup\$
1
\$\begingroup\$

It turns out I should have paid more attention in CS class. I didn't realize Unity overrides any initialized values from scripts with whatever is present in the Inspector. Having made all my variables public, the Inspector was overriding them every time, even when I used [HideInInspector]. Simply making all the variables private (which they should have been from the beginning) solved the problem.

\$\endgroup\$
0
\$\begingroup\$

You may want to consider setting up your transitions based on a time period instead of a multiplier. That way, you can instead adjust the "Duration" of your transition in milliseconds instead of fiddling with multiplier values.

Consider something like this:

float Duration = 0.5f;
const float DURATION = 0.5f;
Vector3 StartSize = Vector3.one;
Vector3 EndSize = Vector3.zero;
bool Done;

Update(){
    if (!Done)
    {
        Duration -= Time.deltaTime;
        if (Duration <= 0) {
            Duration = 0;
            Done = true;
        }
        mySprite.transform.localScale = Vector3.Lerp (EndSize, StartSize, Duration / DURATION);     
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.