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 juanaverdu · May 30, 2020 at 09:09 PM · speedrestart

Speed changes every time I use restart button

I am building a simple snake game since I'm learning how to code and use Unity. I managed to load a "Game over" scene whenever you lose and added a Restart button on that scene, but when I play the game and use that button speed changes everytime. I've been trying to solve this for the last two days and i can't find a solution.

Comment
Add comment · Show 2
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
avatar image KoenigX3 · May 30, 2020 at 09:11 PM 0
Share

Could you post the relevant codes?

avatar image juanaverdu KoenigX3 · May 30, 2020 at 09:26 PM 0
Share

yes, sorry.

 void $$anonymous$$ove()
     {
         Vector2 v = transform.position;
 
         transform.Translate(dir);
          
         if (comio) 
         {
            
             GameObject g = (GameObject)Instantiate(colaPrefab, v, Quaternion.identity);
            
             cola.Insert(0, g.transform);
 
             comio = false;
         }
 
         else if (cola.Count > 0)
         {
             
             cola.Last().position = v;
            
             cola.Insert(0, cola.Last());
             cola.RemoveAt(cola.Count - 1);
         }
         
         
         
     }

"cola" means tail and "comio" is eat, which is a boolean I set to false. "dir" was set to Vector.zero

this gets called here:

 void Start()
     {
         
         InvokeRepeating("$$anonymous$$ove", 8 * Time.deltaTime, 8 * Time.deltaTime);
 
     }
 

And this is how the button works:

 public void RestartGame()
     {
 
         Scene$$anonymous$$anager.LoadScene(1);
 
     }

I don't know if this is helpful but I did most of it following this tutorial

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Eno-Khaon · May 31, 2020 at 01:02 AM

When the Start() function is called in your script, the speed of your game is being made dependent on the exact time it took to process that frame.

 InvokeRepeating("Move", 8 * Time.deltaTime, 8 * Time.deltaTime);


Sometimes, the frame is processed quickly and the game plays out at high speed, while at other times, the frame might experience a performance hiccup and the game slows considerably. For example, if the frame takes 1/40 of a second, the snake will move every 0.2 seconds, but if the frame takes 1/2 of a second, the snake will only move every 4 seconds.

You should probably decide on a rate for it to move at regardless of framerate instead. As an example:

 public float gameSpeed = 4.0f; // 4 moves per second
 
 // ...
 
 float reciprocalSpeed = 1.0f / gameSpeed;
 InvokeRepeating("Move", reciprocalSpeed, reciprocalSpeed);
Comment
Add comment · Share
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

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

129 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

Related Questions

Laggy camera on const moving speed 1 Answer

Can you change the speed of a raycast? 1 Answer

How to get collision force, not relative velocity? 1 Answer

How to get the speed of an object? 3 Answers

physics collision speed problem 2 Answers


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