How do I increase the speed of the pong ball every 5 seconds?

I want to increase the speed of the pong ball every 5 seconds so i tried a few things and nothing works can anyone explain why by giving me a fix?

this is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.WSA;

public class ball_physics : MonoBehaviour
{
    void Start()
    {
        Launch();
        InvokeRepeating("increaseSpeed", 3f, 5f);
    }

    public float ball_speed;
    public Rigidbody2D rb;
    private float new_ball_speed;

    // Update is called once per frame
    void Update()
    {

    }

    private void Launch() 
    {
        float x = Random.Range(0, 2) == 0 ? -1 : 1;
        float y = Random.Range(0, 2) == 0 ? -1 : 1;
        rb.velocity = new Vector2(ball_speed * x +new_ball_speed, ball_speed * y + new_ball_speed);
    }

    void increaseSpeed()
    {
        new_ball_speed = ball_speed + 1f;
        ball_speed += 1f;
    }

}

Hi @super_123hero,

There are a few different ways to handle this, I’ll outline the best approach imo below.

The way we’ll do it is by creating a CountDown class that we can re-use if we need to. This allows us to abide by the single responsibility principle and will give us the ability to fine tune anything that we need.

public class CountDown
{
            
    private float _countDownTimer;
    private float _countDownReset;

    public CountDown(float seconds)
    {
        _countDownTimer = seconds;
        _countDownReset = _countDownTimer;; 
    }

    public void ResetTimer()
    {
        _countDownTimer = _countDownReset;
    }

    public float GetCurrentCountDown => _countDownTimer;

    public bool StartCountDown()
    {
        _countDownTimer -= Time.deltaTime;
        if (!(_countDownTimer <= 0))
            return false;
        
        _countDownTimer = _countDownReset;
        return true;
    }
    
}

To Use:

private CountDown _countDown;

private void Start()
{
    //Create the object and set the seconds
    _countDown = new CountDown(5f); 
}

private void Update()
{
    // If the countDown reaches 0, trigger the action.
    if (_countDown.StartCountDown())
    {
        increaseSpeed();
    }
}

Hope that helps!

Some parts of your code was almost there but you weren’t changing the rb.velocity anywhere but on Start.

BallController.cs

using UnityEngine;

public class BallController : MonoBehaviour
{

	[SerializeField] Rigidbody2D _rigidBody2D;
	[SerializeField][Min(0)] float _speedOnStart = 1;
	[SerializeField][Min(0)] float _speedIncrease = 0.5f;
	public int _speedLevel = 1;

	void Start ()
	{
		// launch:
		float x = Random.Range(0,2) == 0 ? -1 : 1;
		float y = Random.Range(0,2) == 0 ? -1 : 1;
		Vector2 initialDirection = new Vector2(x,y).normalized;
		_rigidBody2D.velocity = initialDirection * _speedOnStart;
		
		// schedule increase speed routine:
		float repeatEverySeconds = 5f;
		InvokeRepeating( nameof(OnIncreaseSpeed) , repeatEverySeconds , repeatEverySeconds );
	}

	void OnIncreaseSpeed ()
	{
		_speedLevel++;

		// apply constant speed:
		float newSpeed = _speedOnStart + _speedLevel * _speedIncrease;
		_rigidBody2D.velocity = _rigidBody2D.velocity.normalized * newSpeed;
		Debug.Log($"new speed level:{_speedLevel}, new speed: {newSpeed:0.0} [m/s]");
	}

}
private float time = 0f;

private void Start()
{
    ...
    time = Time.time;
}

private void Update()
{
    if (Time.time - time >= 5f)
    {
        increaseSpeed();
        time = Time.time;
    }
}