简体   繁体   中英

Unity: Player Jumping Issue

I have a piece of code that is trying to get the player to jump between 2 boxes only Cube 1 (position on left side) and Cube 2 (on right side) by tapping.

The issue with the Move() function is that it starts jumping from Cube 1 to Cube 2, then Cube 2 back to Cube 1, but from this point on, the player jumps from Cube 1 to the left side the opposite of Cube 2.

The jump functions are working, but I think the logic is incorrect.

Move:

void Move(){
    int i = 0;
    while ((isGrounded == true) && (i < 10)) {

        if(atCube1 == true){
            JumpRight();
        }

        if(atCube2 == true){
          JumpLeft();
        }

        i++;
    }
}

OnCollisionEnter:

void OnCollisionEnter (Collision col)
{
    Debug.Log("OnCollisionEnter");


    if (col.gameObject.name == "Cube 1"){
        Debug.Log ("++++++ C U B E 1   H I T ++++++++");

        atCube1 = true;
        isGrounded = true;

    }

    if(col.gameObject.name == "Cube 2"){

        Debug.Log ("Cube 2 hit");
        atCube2 = true;
        isGrounded = true;

    }
}

You should make atCube1 = false; after it left cube1(do it for cube2 too). Also you can add isGrounded = false; when it does not collides anything.

1-You start at cube1, atCube1 = true

2-Jump to cube2, atCube1 = true , atCube2 = true

3- Jump to cube1 again, it jumps left because still atCube2 = true . In fact, it jumps right at first and then jumps left because of your if statements' order. Just add false values to booleans to fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM