Je moder
Je moder

Reputation: 83

Does someone know what's wrong in my if statement in C# Unity3D?

I want to change the int speed value, but it doesn't work. Can someone help me? If you too have too few information, pls ask me. This is my code:

    if (Input.GetKeyDown (KeyCode.W)) {
        sprint1 = true;
    } else if (Input.GetKeyUp(KeyCode.W)){
        sprint1 = false;
    } 
    if (Input.GetKeyDown(KeyCode.LeftShift)){
        sprint2 = true;
    } else if (Input.GetKeyUp(KeyCode.LeftShift)){
        sprint2 = false;
    }
     if (sprint2 == false && Input.GetKeyUp(KeyCode.W) && Input.GetKeyUp(KeyCode.A) && Input.GetKeyUp(KeyCode.S) && Input.GetKeyUp(KeyCode.D)){
        speed = 0;
    } if (sprint2==false && (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D))){

        speed = 4;

    }if (sprint1 == true && sprint2 == true && Scoped==true) {
        speed = 8;
    }

Upvotes: 0

Views: 642

Answers (4)

Je moder
Je moder

Reputation: 83

I have fixed my own issue. Thanks for all your comments, but I wanna show you my solution:

     if (Input.GetKey (KeyCode.W)) {
        sprint1 = true;
    } else {
        sprint1 = false;
    }
    if (Input.GetKey (KeyCode.LeftShift)) {
        sprint2 = true;
    } else {
        sprint2 = false;
    }
    if (sprint2 == false && sprint1==false && !(Input.GetKey (KeyCode.A)) && !(Input.GetKey (KeyCode.S)) && !(Input.GetKey (KeyCode.D))){
        speed = 0;
    } if ((sprint1==true && sprint2==false) || Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.D)){

        speed = 4;

    }if (sprint1 == true && sprint2 == true && Scoped==true) {
        speed = 8;
    }

Upvotes: 0

Brandon Ripley
Brandon Ripley

Reputation: 1

Here is a way of doing it so you don't have to make all these unnecessary flags for sprint.

    if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift) && Scoped)
        speed = 8;
    else if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S))
        speed = 4;
    else
        speed = 0;

Upvotes: 0

Chuck Savage
Chuck Savage

Reputation: 11955

If a key isn't down, it's up. No need to check for both. Anyway, this is the way I interpret your if.

sprint1 = Input.GetKeyDown(KeyCode.W);
sprint2 = Input.GetKeyDown(KeyCode.LeftShift);

if (sprint2)
{
   if (sprint1 && Scoped)
        speed = 8;
     else
        speed = 4; // do you want speed 0 or 4 if either of these others are false
}
else
{
    if (sprint1
     || Input.GetKeyDown(KeyCode.A) 
     || Input.GetKeyDown(KeyCode.S) 
     || Input.GetKeyDown(KeyCode.D))
        speed = 4;     
    else
        speed = 0; 
}

What I think would work better, if I'm interpreting your desire, is

sprint1 = Input.GetKeyDown(KeyCode.W);
sprint2 = Input.GetKeyDown(KeyCode.LeftShift);

if (sprint1 && sprint2 && Scoped)
     speed = 8;
else if (sprint1
     || Input.GetKeyDown(KeyCode.A) 
     || Input.GetKeyDown(KeyCode.S) 
     || Input.GetKeyDown(KeyCode.D))
    speed = 4;     
else
    speed = 0; 

Upvotes: 0

Izuka
Izuka

Reputation: 2612

if (sprint2 == false && Input.GetKeyUp(KeyCode.W) && Input.GetKeyUp(KeyCode.A) && Input.GetKeyUp(KeyCode.S) && Input.GetKeyUp(KeyCode.D)){
    speed = 0;
}

You will never enter in this particular case except if, during the exact same frame, you release W, A, S and D. Look at the Input.GetKeyUp documentation:

Returns true during the frame the user releases the key identified by the key KeyCode enum parameter.

You should organise it this way instead:

// If you are sprinting, set the speed to 8
if (sprint1 == true && sprint2 == true && Scoped==true) {
    speed = 8;
}
// Else if one of those keys has been touched during this frame, set the speed to 4
else if (sprint2==false && (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))){
    speed = 4;
}
// Else, just set the speed to 0
else
    speed = 0;

Upvotes: 2

Related Questions