bulb
bulb

Reputation: 188

Unity - Make text expand then contract like popping towards the user. I want to draw the uers attention to their score

I am looking for a way to make the users score which is a UI text component to pop. What I mean is expand then contract and maybe shake on each score increase.

I am not really sure how to start. I have the score incrementing and I am guessing I can change the size of the text object and then make it back to the original size but it does not look so good doing this. Any advice would be welcomed.

Upvotes: 0

Views: 147

Answers (1)

Ruzihm
Ruzihm

Reputation: 20269

I would use a coroutine to change the localScale of the text's rectTransform using Mathf.PingPong:

// Recommendation: assign Text here in the inspector
public Text textToScale;
public float halfDuration = 0.5f;    
public float bigScaleMultiplier = 1.5f;

private Coroutine effectCoroutine = null;
private Vector3 scaleStart;

// call this to begin the effect
public void StartEffect()
{
    // ensure duplicates don't run
    if (effectCoroutine == null)
    {
        effectCoroutine = StartCoroutine(DoEffect());
    }
}

private IEnumerator DoEffect()
{
    float elapsedPortion = 0f;
    scaleStart = textToScale.rectTransform.localScale;
    float scaleMultiplierIncrease = bigScaleMultiplier - 1f;

    while (elapsedPortion < 2f)
    {
        // t = What fraction of "big" we are currently at
        float t = Mathf.PingPong(elapsedPortion, 1f);

        float curScaleMultiplier = 1f + scaleMultiplierIncrease * t;

        textToScale.rectTransform.localScale = curScaleMultiplier * scaleStart;
        yield return null;
        elapsedPortion += Time.deltaTime / halfDuration;
    }

    textToScale.rectTransform.localScale = scaleStart;
    effectCoroutine = null;
}

Upvotes: 2

Related Questions