- Home /
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.
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?
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
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.
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.
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.
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
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.
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.
The problem was related to when you were resetting startTime and $$anonymous$$axSpeed.
Under the problem code here is what happens
Time.time and $$anonymous$$axSpeed increase as designed
User collides with "zid"
$$anonymous$$axSpeed is set to 12
Level is loaded
Update runs for several frames using the old startTime, setting $$anonymous$$axSpeed high
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.
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.
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
Follow this Question
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