Page 1 of 1

I want to make the characterpersecond of the typewrittereffect to go faster if i held a button

Posted: Tue Jul 23, 2024 4:12 pm
by picturatheartist
I'm trying to setup the typical scenario when you want the text to be typed faster when pressed a certain button, in my case A from gamepad.

I set this on script to control it

public void SetSpeedBoost(bool isBoosted)
{
this.isBoosted = isBoosted;
if (typewriterEffect != null)
{
float newSpeed = isBoosted ? defaultSpeed * boostedSpeedMultiplier : defaultSpeed;
typewriterEffect.SetSpeed(newSpeed);
Debug.Log("Setting new speed");
}
else
{
Debug.LogError("TypewriterEffect no está asignado.");
}
}

i tried different ways to do it but according to the guide it needs to be made like this, but im having the problem that, yes, it goes faster when pressing the button, but when released, it waits a second or some time and then continues with the default speed.

any idea how to solve this?

Re: I want to make the characterpersecond of the typewrittereffect to go faster if i held a button

Posted: Tue Jul 23, 2024 8:59 pm
by Tony Li
Hi,

Make a subclass of your typewriter effect. Then override the SetSpeed() method to something like (for TMPro):

Code: Select all

public class MyTypewriterEffect : TextMeshProTypewriterEffect
{
    public override void SetSpeed(float charactersPerSecond)
    {
        base.SetSpeed(charactersPerSecond);
        if (IsPlaying)
        {
            StopTypewriterCoroutine();
            StartTypewriterCoroutine(charactersTyped);
        }
    }
}
By stopping and restarting the typewriter effect, it will recompute the delays needed in the coroutine.