Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Can an object script NOT contain Update()? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges