0
\$\begingroup\$

I'm a a beginner, creating an endless runner game.

I want to increase speed as the player completed the distance and on every 500 meter speed should be increased. Anyone have an idea of how to implement this kind of logic?

speed=10;
//as soon as player reach 500 m distance
//speed should be incremented
//it should be continuous process on every 500m 

My code:

public class Score : MonoBehaviour
{
    [SerializeField]
    GameObject ground;
    [SerializeField]
    AudioClip clip;
    [SerializeField]
    Transform player;
    [SerializeField]
    public Text LiveScore;
    [SerializeField]
    public GameObject gameOver;
    [SerializeField]
    public bool runningScore = true;
    public Text finalScore;
    public Text Highscore, bestScore;
    float distance;
    ball Ball;
    // Start is called before the first frame update
    private void Start()
    {

        Ball = GetComponent<ball>();
        //  PlayerPrefs.DeleteKey("HighScore");
        Highscore.text = "Best :" + ((int)PlayerPrefs.GetFloat("HighScore", 0)).ToString();
        bestScore.text = "Best :" + ((int)PlayerPrefs.GetFloat("HighScore", 0)).ToString();
    }
    // Update is called once per frame
    void Update()
    {


        distance = player.position.z + 500;
        if (runningScore)
        {
            LiveScore.text = distance.ToString("0");

            //  Highscore.text = LiveScore.text;
        }
        if (runningScore == false)
        {
            float score = float.Parse(LiveScore.text);
            finalScore.text = "Score:" + LiveScore.text;
            if (PlayerPrefs.GetFloat("HighScore", 0) < score)
            {

                PlayerPrefs.SetFloat("HighScore", score);
                Highscore.text = "Best :" + score.ToString();

                bestScore.text = Highscore.text;
                AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position);
            }
            else if (score < PlayerPrefs.GetFloat("HighScore", 0))
            {

                bestScore.text = Highscore.text;
            }


        }

        while (distance > 500)
        {
            Ball.speed += 5;
        }


    }

}
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

speed = startSpeed + (int)(trackedDistance / 500) * speedIncrement;

Now your new question is, "how to track distance of player", Since its endless runner game:

trackedDistance += speed * Time.deltaTime

Ball.speed = speed;

Your welcome.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ "You're welcome". \$\endgroup\$
    – Engineer
    Apr 1, 2020 at 14:34
  • \$\begingroup\$ @nick !! shall i use loop \$\endgroup\$ Apr 1, 2020 at 14:48
  • \$\begingroup\$ @MustafaAhmed no, just put this code into Update() function. \$\endgroup\$
    – Nick
    Apr 1, 2020 at 17:53
  • \$\begingroup\$ @nick tracked distance should be length of the track? \$\endgroup\$ Apr 1, 2020 at 17:57
  • \$\begingroup\$ @MustafaAhmed trackedDistance should be 0 at beginning of game, and it will increase automatically towards infinity every Update() call(that Unity calls every frame) by your calculated "speed" value \$\endgroup\$
    – Nick
    Apr 1, 2020 at 17:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .