Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 lukasyu · May 23, 2014 at 11:42 AM · speedifvalues

Cannot reduce speed to start speed

Basically I'm making a game where the player object (a ball) avoids some obstacles by jumping and such. When the game begins the ball accelerates to its MaxSpeed(a private var), which increases by 2 every 30 seconds, making the game a bit harder, but when the ball collides with the obstacle, the game should restart, reset the score, and reduce the MaxSpeed back to MaxSpeed1, which is the starting limit. While the first 2 things work, the last one doesn't. Any ideas? . . .

 var MaxSpeed1:float=12;
 private var MaxSpeed:float=MaxSpeed1;
 .
 .
 .
 if(Time.time-startTime>nextSpeedtime)
  {
      MaxSpeed+=2.0;
      nextSpeedtime+=speedPeriod;
  }
 .
 .
 .
 function OnCollisionEnter(col: Collision){
   if (col.transform.name == "zid"){
   
       MaxSpeed=MaxSpeed1;
   
    
     Application.LoadLevel(Application.loadedLevel);
     
     currentScore=0;
   }
   }

Note that everything works like a charm, apart from the "MaxSpeed=MaxSpeed1"

edit:I also tried doing something as simple as "MaxSpeed=12", but it continues growing.

Comment
Add comment · Show 5
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 Nezabyte · May 23, 2014 at 01:50 PM 0
Share

Does it continue growing from where it was last? So if they were at 24 speed when the game ended, it stays at 24 then continues growing?

avatar image NoseKills · May 23, 2014 at 10:14 PM 1
Share

Are you resetting startTime somewhere ?

avatar image lukasyu · May 24, 2014 at 10:44 PM 0
Share

Yeah, sorry, I didn't want to post the entire code, because it's kind of huge (I started unity about 3 days ago so I guess it could have been smaller), but I do indeed reset time, the ball stops moving when the level resets, and then I use this:

 if(Input.Get$$anonymous$$ey("right"))
  {
      startTime=Time.time;
      still=false;
      rigidbody.useGravity=true;
  }

But when I put my $$anonymous$$axSpeed as just var so that I can check its value I see that it simply doesn't accept the $$anonymous$$axSpeed=$$anonymous$$axSpeed1 part, when the lever resets, it continues, as Nezabyte says, growing from where it was last, and then continues growing

avatar image cryingwolf85 · May 24, 2014 at 11:07 PM 1
Share

First off: Why are you using Get$$anonymous$$ey to detect a single key press? You should just have Get$$anonymous$$eyDown.

Second off: Why are you doing that anyway? Don't do that: put the lines that you just wrote (The ones inside the if statement) in the same area where the collision occurs:

 function OnCollisionEnter(col: Collision){
   if (col.transform.name == "zid"){
  
     $$anonymous$$axSpeed=$$anonymous$$axSpeed1;

     // This is the important line
     startTime=Time.time;

     still=false;
     rigidbody.useGravity=true;

     currentScore=0;

     Application.LoadLevel(Application.loadedLevel);
 }

And get rid of the key thing.

avatar image lukasyu · May 24, 2014 at 11:16 PM 0
Share

As for the key thing, you're right, as I said, I'm pretty new, so I'm still getting used to things, but as for the startTime=Time.time, $$anonymous$$y goal is not to put the starttime when I reset the level, but when I reset the level AND click the "begin button", which for me is the right arrow key, I hope you understand what I mean.

Basically The game starts with the ball floating in the air, when i click the right button, the gravity turns on, making the ball fall down, the start time initializes, and the still bool is marked as false, meaning the ball is moving, used for some other commands(Like the actual movement).

I hope this doesn't come off as rude or anything, since I'm the one asking for help, but I'm assuring you, the time is not the problem, because it's the $$anonymous$$axSpeed that stays the same after the level resets, and I don't have a single line in the code which affects the $$anonymous$$axSpeed1, the var that keeps the original $$anonymous$$axSpeed value.

edit: I found another mistake, after restarting level I don't initialize still=true; but it still doesn't solve the problem.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiwasi · May 24, 2014 at 03:05 AM

This is happening because Time.time measures time from the start of the game, not since the start of the level.

You need to add the lines

 startTime = Time.time;
 nextSpeedTime = startTime + speedPeriod;

At about line 16.

Comment
Add comment · Show 2 · 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
avatar image lukasyu · May 24, 2014 at 10:47 PM 0
Share

Sorry, but that's not the problem I'm afraid, I never reset the nextSpeedTime, and the issue is with $$anonymous$$axSpeed not obtaining its old(new) value, I can see it in the Inspector tab

avatar image Kiwasi · May 25, 2014 at 07:24 AM 0
Share

This does work with the original post code. Since you didn't mention otherwise I assumed you were starting the level immediately on Application.LevelLoad().

For future questions make sure you post all the code that can change the variables in use. On the other hand thanks for recognising that posting the entire project was not needed.

avatar image
0

Answer by lukasyu · May 24, 2014 at 11:55 PM

I solved the problem by putting the MaxSpeed=MaxSpeed1 line in the if(Input.GetKeyDown("right")) { //MaxSpeed=MaxSpeed1; startTime=Time.time; still=false; rigidbody.useGravity=true; } which starts the game(movement) itself, although I'd still really like to know why the first one doesn't work, it just doesn't make sense to me, thank you in advance.

Comment
Add comment · Show 3 · 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
avatar image Kiwasi · May 25, 2014 at 07:19 AM 0
Share

The problem was related to when you were resetting startTime and $$anonymous$$axSpeed.

Under the problem code here is what happens

  1. Time.time and $$anonymous$$axSpeed increase as designed

  2. User collides with "zid"

  3. $$anonymous$$axSpeed is set to 12

  4. Level is loaded

  5. Update runs for several frames using the old startTime, setting $$anonymous$$axSpeed high

  6. User presses the "right" key. startTime is reset. $$anonymous$$axSpeed stays high.

$$anonymous$$ake sense?

The key to the solution is that startTime and $$anonymous$$axSpeed must be reset at the time the player begins to play the level.

avatar image lukasyu · May 25, 2014 at 09:46 AM 0
Share

I (hopefully)see what you are saying, but I'm still unsure, I'll tell you why.

  if(Time.time-startTime>nextSpeedtime)
      {
          $$anonymous$$axSpeed+=2.0;
          nextSpeedtime+=speedPeriod;
      }

Let's say the level is reloaded, and I didn't press the right button immediately, so what you are saying is, because I do not reset the startTime right away, Time.time-startTime may pass the nextSpeedtime, and my $$anonymous$$axSpeed may increase by 2, and it can affect the game so thank you for pointing that out, but it can never reach so much in a short period of time. In my inspector tab, when the lever resets, $$anonymous$$axSpeed never changes.

So, for example, if it reaches 50 eventually, it will stay 50, and then start increasing from there again. I apologize if I misunderstood what you were saying, and thanks for trying to figure this out.

avatar image Kiwasi · May 25, 2014 at 07:43 PM 0
Share

Sorry I missed an important detail. Unless they are static or have Don'tDestroyOnLoad startTime and nextSpeedTime will reset to the original values of 0 on Application.LoadLevel. Time.time will not.

Hence your if loop in the update will trigger on the first x frames until Time.time-startTime < nextSpeedtime.

You won't see the change in the inspector because the inspector is not updated every frame. One way to see this happen would be to put Debug.Log ($$anonymous$$axSpeed) inside the if loop

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

24 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

Related Questions

if condition question... 1 Answer

How can I get normal velocity value? 0 Answers

Use multiple values for "if" statement 1 Answer

Flight Sim Control 1 Answer

Pathfinding with specific end-direction and turning speed limit 0 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