Block Breaker Speed Increase

What I am trying to do is boost the speed in the paddle by pressing a button in Block Breaker. My last post was closed so I had to make a new one.
Here is the code I am using.
‘’'public class MoveLR : MonoBehaviour
{

public float PlayerSpeed = 5;
bool mofe = false;

// Start is called before the first frame update
void Start()
{
}

// Update is called once per frame
void Update()
{
    float amtToMove = Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime;
    transform.Translate(Vector3.right * amtToMove);
}
private void Mofast()
{
    if (CrossPlatformInputManager.GetButtonDown("Fire1"))
    {
        PlayerSpeed = 20f;
    }
}

}
/* mofe = PlayerSpeed = 20 How to temporarily increase the speed of a character in a Unity game - Game Development Stack Exchange */
‘’’
This is the old post.
How to apply code formatting within your post

1 Like

Hi!

Try to explain your issue as detailed as possible, because I don’t know what I’m answering here so I’ll end up writing a wall of text without even knowing if that’ll help you.

From what I’m seeing your idea is lacking many things.

Game Design Wise

  • Have you considered a cooldown? The code shown in the links provided should work, but you can spam the button and keep the speed boost forever, not sure if that’s what you are going for.

CodeWise

  • Your Mofast method isn’t being called anywhere, that should be called inside an update call.
  • The speed will remain at 20, it will never get back to the default, which according to your code is 5.

Here’s an easy example of how to make it work with a cooldown, not my favorite approach, but a simple to understand one based on what you already researched.

[SerializedField] float defaultSpeed = 5f; 
[SerializedField] float boostSpeed = 20f; //Make it a variable, don't use magic numbers
[SerializedField] float boostTime = 5f;
[SerializedField] float cooldownTime = 3f;

bool mofe = false;
float playerSpeed;

private void Awake()
{
    playerSpeed = defaultSpeed;
}

private void Update()
{
    float amtToMove = Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime;
    transform.Translate(Vector3.right * amtToMove);

    MoFast();
}

private void Mofast()
{
    if (CrossPlatformInputManager.GetButtonDown("Fire1") && !mofe)
    {
        mofe = true
        PlayerSpeed = boostSpeed;
        Invoke("ResetSpeed", boostTime);
    }
}

private void ResetSpeed()
{
    playerSpeed = defaultSpeed;
    Invoke("EndCooldown", cooldownTime);
}

private void EndCooldown()
{
    mofe = false;
}

That code should work, it might have some syntax errors because I wrote it here, without an IDE, so I might be missing a semicolon.

Privacy & Terms