D
D
dani220232021-01-19 10:12:40
C++ / C#
dani22023, 2021-01-19 10:12:40

How to make the character's speed increase every n seconds?

How to make the character's speed increase every n seconds? I have a cyclist simulator game, and after, for example, 5 seconds, the character’s speed was not 1, as it was originally, but 2.
Code for character movement:

using UnityEngine;
using System.Collections;
public class NewBehaviour : MonoBehaviour
{
    public GameObject player;
    public int speedRotation = 3;
    public int speed = 5;
    public int jumpSpeed = 50;

    void Start()
    {
        player = (GameObject)this.gameObject;
    }
    void Update()
    {

        {
             player.transform.position += (-player.transform.right) * speed * Time.deltaTime;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
H
HemulGM, 2021-01-19
@HemulGM

It is better to increase it gradually, and not "after 5 seconds"

F
freeExec, 2021-01-19
@freeExec

totalTime += Time.deltaTime;
if (totalTime > 5)
   speed *= 1000;

V
Vladimir Korotenko, 2021-01-19
@firedragon

Make all speeds float. Start a timer at startup and add 0.1 to your speed every 0.1 second. After exceeding 2, stop the timer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question