Reputation:
void OnMouseDown () {
if(name == "SlowButton"){
PlaneMovement.GameSpeed = 0.5f;
}
if(name == "MediumButton"){
PlaneMovement.GameSpeed = 1f;
}
if(name == "FastButton"){
PlaneMovement.GameSpeed = 2f;
}
if(name == "VeryFastButton"){
PlaneMovement.GameSpeed = 3f;
}
Application.LoadLevel("game");
}
I enter with this code into the Next Scene and there i have a code for some extra Power like speedup while collision occurs for that the code is:
if(C.tag == "Dimond"){
Camera.main.GetComponent<StoneInitiate>().DimondList.Remove(C.gameObject);
Destroy (C.gameObject);
Camera.main.GetComponent<StoneInitiate>().StoneCounter+=20;
smooth = 4f;
SpeedUpTimer = 5f;
SpeedUp = true;
}
speedup code is:
PlaneMovement.GameSpeed = Mathf.Lerp (PlaneMovement.GameSpeed,smooth,Time.time);
if(SpeedUp){
SpeedUpTimer -= Time.deltaTime;
print(SpeedUpTimer);
if(SpeedUpTimer <= 0){
SpeedUp = false;
smooth = PlaneMovement.GameSpeed;
}
It Works Fine when collision occurs but smooth takes value 4f and It Doesn't get value form
PlaneMovement.GameSpeed
please help me for that I am new in Unity. Thanks in advance.
Upvotes: 1
Views: 101
Reputation:
I just Do this and it works..
float smooth;
float smooth1;
void Start () {
smooth = PlaneMovement.GameSpeed;
smooth1 = PlaneMovement.GameSpeed;
}
PlaneMovement.GameSpeed = Mathf.Lerp (PlaneMovement.GameSpeed,smooth,Time.deltaTime);
if(SpeedUp){
SpeedUpTimer -= Time.deltaTime;
if(SpeedUpTimer <= 0){
SpeedUp = false;
smooth = smooth1;
}
Upvotes: 0
Reputation: 1422
The problem is here
PlaneMovement.GameSpeed = Mathf.Lerp (PlaneMovement.GameSpeed,smooth,Time.time);
if(SpeedUp){
SpeedUpTimer -= Time.deltaTime;
print(SpeedUpTimer);
if(SpeedUpTimer <= 0){
SpeedUp = false;
smooth = PlaneMovement.GameSpeed;
}
}
You are using Lerp incorrectly, the first argument is the From value, the second is the To value and the third is how far you go between From and To, lets call it t.
So for example, say that your From = 1 and To = 2.
If t = 0, then it will return From and thus 1
if t = 1 then it will return To and thus 2
if t = 0.5 then it will return the value half way between 1 and 2 thus 1.5
t is clamped to 0 and 1
You use Time.time which is a number larger then 1 which means that it will always return your smooth. thus GameSpeed will always be equal to smooth after you call
PlaneMovement.GameSpeed = Mathf.Lerp (PlaneMovement.GameSpeed,smooth,Time.time);
What you want to do is call that line several times with a t of something like 0.5
Lets say GameSpeed is 1 and smooth is 4. When you call this
PlaneMovement.GameSpeed = Mathf.Lerp (PlaneMovement.GameSpeed,smooth,0.5f);
GameSpeed will now be 2.5. If you call it again with GameSpeed now beeing 2.5 it will become 3,25 and a third time for 3,625. The more you call it the close it will get to 4.
As Rudolfwm pointed out you can make this framerade independant by using the delta time like this
PlaneMovement.GameSpeed = Mathf.Lerp (PlaneMovement.GameSpeed,smooth,0.5f * Time.deltaTime);
Upvotes: 1