How To Limit Max Speed While Making Game Progressively Faster?

How To Limit Max Speed While Making Game Progressively Faster?

Hey Everyone,
I need my game to get progressively faster whilst also limiting the speed so the player doesn’t fly into newly spawned objects.

Its a bit like the game Slope.

Thanks in Advance.

Code

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

public class PlayerMovement : MonoBehaviour
{
    public float strafeSpeed;
    public float forwardSpeed;

    public float maxSpeed;
    public float speedModifier = 1f;
    Rigidbody rb;

    public bool isDead;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        isDead = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (isDead == false)
        { 
            rb.velocity += new Vector3(Input.GetAxis("Horizontal") * strafeSpeed * speedModifier * Time.deltaTime, 0, forwardSpeed * speedModifier * Time.deltaTime);

            if (rb.velocity.magnitude > maxSpeed)
            {
                rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
            }
        }    
    }

    void OnCollisionEnter (Collision collisionInfo)
    {
        if (collisionInfo.collider.CompareTag("DeathObject"))
        {
           isDead = true;
           forwardSpeed = 0;
           strafeSpeed = 0;
        }
    }
    
}

You could use if statement. However you need a speed vairable first. Let’s assume you move your player with this transform.Translate (I can only assume since you haven’t posted any code).

Then you could do sometihng like this:

using UnityEngine;

public class accelarate : MonoBehaviour
{
    public float speed,MaxSpeed;
    public float accelarateAmount;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //Or FixedUpdate and DeltaTime
        transform.Translate(0, speed * Time.fixedDeltaTime, 0);

        if (speed < MaxSpeed)
        {
            speed += accelarateAmount;
        }
        //Also if speed overshoots the MaxSpeed force it to decrease

        if (speed > MaxSpeed)
        {
            speed = MaxSpeed;
        }

    }
}

However if you want to game faster by playing with playing time speed:

using UnityEngine;

public class accelarate : MonoBehaviour
{
    public float speed, MaxTimeSpeed,time;
    public float TimeAdditionAmount;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        time = Time.timeScale;
        //Or FixedUpdate and DeltaTime
        transform.Translate(0, speed * Time.fixedDeltaTime, 0);

        if (time < MaxTimeSpeed)
        {
            time += MaxTimeSpeed;
        }
        //Also if speed overshoots the MaxSpeed force it to decrease

        if (time > MaxTimeSpeed)
        {
            time = MaxTimeSpeed;
        }

    }
}

Hope tihs helps.

Hey @New_Game_Ideas , thanks for the help.

However, I am using a Rigidbody and its velocity to control the player so I hope the same principle applies to what you’ve done.

Sorry, I forgot to upload the code. Here it is.

I managed to limit the speed, because there is a maxSpeed variable, it wont get any faster than that. So do I increase maxSpeed as the game gets harder?

Thanks.