If statement being ignored
For some reason my if statement is being ignored. Whenever I charge up my for a long enough time, it just does the normal dash, even though according to the prefab it's true. The ignored line is at 30.
//Dash Cooldown Timer
if(DashCD > 0){
DashCD -= Time.deltaTime;
}
if(DashCD < 0){
DashCD = 0;
}
//Dash Charging Timer
if(Input.GetKey(KeyCode.LeftShift)) {
DashChargingTime += Time.deltaTime;
DashCharging = true;
}
if(Input.GetKeyUp(KeyCode.LeftShift)) {
DashChargingTime = 0;
DashCharging = false;
}
if(DashChargingTime > DashChargeTime) {
this.gameObject.GetComponent<PlayerSprites>().Charged = true;
ChargeDashReady = true;
}
else {
this.gameObject.GetComponent<PlayerSprites>().Charged = false;
ChargeDashReady = false;
}
if(Input.GetKeyUp(KeyCode.LeftShift) && DashCD == 0){
if(Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.W)) {
if (DashCD == 0) {
if(ChargeDashReady == true) { //The if statement that is being ignored
Debug.Log("Charged Dash"); //The outcome that it should go to
RB.AddForce(Vector2.up * DashSpeedVert, ForceMode2D.Impulse);
RB.AddForce(Vector2.left * DashSpeedVert, ForceMode2D.Impulse);
DashCD = DashCDTime;
ChargedDash = true;
}
else {
Debug.Log("Not Charged Dash"); //The outcome that it goes to
RB.AddForce(Vector2.left * DashSpeedVert, ForceMode2D.Impulse);
RB.AddForce(Vector2.up * DashSpeedVert, ForceMode2D.Impulse);
DashCD = DashCDTime;
}
}
}
}
Does it print "not charged dash" or "Charged Dash" to the console?
Yes. I just posted the important parts of the code.
Answer by Thaun_ · Mar 18, 2018 at 07:21 PM
It seems that ChargeDashReady turns to false when you release the shift button.
if(Input.GetKey(KeyCode.LeftShift)) {
DashChargingTime += Time.deltaTime;
DashCharging = true;
}
if(Input.GetKeyUp(KeyCode.LeftShift)) { //<< Check Here
DashChargingTime = 0; //<< Check Here
DashCharging = false;
}
if(DashChargingTime > DashChargeTime) { //<< Check Here
this.gameObject.GetComponent<PlayerSprites>().Charged = true;
ChargeDashReady = true;
} else {
this.gameObject.GetComponent<PlayerSprites>().Charged = false;
ChargeDashReady = false; //<< Check Here
}
if(Input.GetKeyUp(KeyCode.LeftShift) && DashCD == 0){ //<< Check Here
DashChargingTime > DashChargeTime turns to false when you release the left shift key which turns ChargeDash to false, which the rest of the code starts to run when you release the button.
Maby if you try:
if(Input.GetKey(KeyCode.LeftShift) && DashCD == 0){
instead
EDIT: Oh, nvm seems he got it fixed.
Answer by DarkToadster · Mar 18, 2018 at 05:47 PM
Checking booleans in an IF Statement do not require any more informations.
Im not sure but this may cause your error.
What i mean is this :
if(isThisTrue) // You do not need == "true" for booleans
{
DoSomething();
}
else
{
DoSomethingElse();
}
@DarkToadster this is definitely not the problem. There's nothing wrong with using:
if(isThisTrue == true) // == true is optional but it's not wrong using it.