- Home /
Question by
SolidSlish · Mar 28, 2020 at 07:48 PM ·
optimizationupdatespeedtimescaleendless runner
Increase Players Speed Overtime in Endless Runner? Is my Code Unoptimized?
I'm currently attempting to make a game similar to subway surfers. Currently, I'm using timescale which I now realize may be a bad idea as this is a mobile game. Here is my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
public class GameManager : MonoBehaviour
{
private const int COIN_SCORE_AMOUNT = 3;
private const int OBSTACLE_DODGE_AMOUNT = 10;
public static GameManager Instance { set; get; }
private bool isGameStarted = false;
private PlayerMotor motor;
// UI and UI Fields
public Text scoreText, coinText, modifierText, hiscoreText;
public float score, coinScore, modifierScore, timeScore;
private int lastScore;
public int difficultyLevel = 0;
public double TimeLevel = 0.04;
private int maxDifficultyLevel = 20;
private int scoreToNextLevel = 10;
private bool isDead = false;
public GameObject flash;
public static bool GameIsPaused = false;
public GameObject pauseMenuUI;
public Button PauseButton;
public Button ResumeButton;
// Death Menu
public Animator deathMenuAnim;
public Text deadscoreText, deadcoinText;
private void Awake()
{
Instance = this;
modifierScore = 1;
motor = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMotor>();
flash = GameObject.Find("Tap Anywhere to Start");
pauseMenuUI.gameObject.SetActive(false);
PauseButton.gameObject.SetActive(true);
modifierText.text = "x" + modifierScore.ToString("0.0");
coinText.text = coinScore.ToString("0");
scoreText.text = scoreText.text = score.ToString("0");
hiscoreText.text = PlayerPrefs.GetInt("Hiscore").ToString();
Application.targetFrameRate = -1;
}
private void Update()
{
if (MobileInput.Instance.Tap && !isGameStarted)
{
isGameStarted = true;
motor.StartRunning();
PoolsManager.poolManager.StartGame();
Time.timeScale = 1f;
flash.SetActive(false);
}
if (isGameStarted)
{
// Bump the Score Up
score += (Time.deltaTime * modifierScore);
if (lastScore != (int)score)
{
lastScore = (int)score;
scoreText.text = score.ToString("0");
}
}
{
if (isDead)
return;
if (score >= scoreToNextLevel)
LevelUp();
score += Time.deltaTime * difficultyLevel;
scoreText.text = ((int)score).ToString();
}
void LevelUp()
{
if (difficultyLevel == maxDifficultyLevel)
return;
scoreToNextLevel *= 2;
difficultyLevel++;
modifierScore += 1;
Time.timeScale += 0.04f;
timeScore += 0.04f;
}
}
public void GetCoin()
{
coinScore++;
coinText.text = coinScore.ToString("0");
score += COIN_SCORE_AMOUNT;
scoreText.text = scoreText.text = score.ToString("0");
}
public void DodgeObstacle()
{
score += OBSTACLE_DODGE_AMOUNT;
scoreText.text = scoreText.text = score.ToString("0");
}
public void UpdateModifier(float modifierAmount)
{
modifierScore = 1.0f + modifierAmount;
modifierText.text = "x" + modifierScore.ToString("0.0");
}
public void OnPlayButton()
{
UnityEngine.SceneManagement.SceneManager.LoadScene("Hallway Dash");
}
public void OnMenuButton()
{
UnityEngine.SceneManagement.SceneManager.LoadScene("Menu");
Time.timeScale = 1f;
}
public void OnDeath()
{
FindObjectOfType<AudioManager>().Play("Death");
isDead = true;
deadscoreText.text = score.ToString("0");
deadcoinText.text = coinScore.ToString("0");
deathMenuAnim.SetTrigger("Dead");
modifierScore = 0f;
if (score > PlayerPrefs.GetInt("Hiscore"))
{
float s = score;
if (s % 1 == 0)
s += 1;
PlayerPrefs.SetInt("Hiscore", (int)score);
}
}
public void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = timeScore;
GameIsPaused = false;
PauseButton.gameObject.SetActive(true);
pauseMenuUI.gameObject.SetActive(false);
}
public void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
PauseButton.gameObject.SetActive(false);
pauseMenuUI.gameObject.SetActive(true);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to run Update multiple times per frame for fast-forwarding purposes 3 Answers
even just cubes moving seems choppy 1 Answer
Need help optimizing grid initialization. 1 Answer
2.5ms update 2 Answers