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

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
picturatheartist
Posts: 1
Joined: Tue Jul 23, 2024 4:08 pm

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

Post 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?
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

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

Post 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.
Post Reply