// Update is called once per frame void Update() { float x = Input.GetAxis("Horizontal"); float y = Input.GetAxis("Vertical"); float xRaw = Input.GetAxisRaw("Horizontal"); float yRaw = Input.GetAxisRaw("Vertical"); dir = new Vector2(x, y); Walk(dir); anim.SetHorizontalMovement(x, y, rb.velocity.y); //if (coll.onWall && Input.GetButton("Fire3") && canMove) //{ // if(side != coll.wallSide) // anim.Flip(side*-1); // wallGrab = true; // wallSlide = false; //} //if (Input.GetButtonUp("Fire3") || !coll.onWall || !canMove) //{ // wallGrab = false; // wallSlide = false; //} if (coll.onGround && !isDashing) { wallJumped = false; GetComponent <BetterJumping>().enabled = true; } if (wallGrab && !isDashing) { rb.gravityScale = 0; if (x > .2f || x < -.2f) { rb.velocity = new Vector2(rb.velocity.x, 0); } float speedModifier = y > 0 ? .5f : 1; rb.velocity = new Vector2(rb.velocity.x, y * (speed * speedModifier)); } else { rb.gravityScale = 3; } if (coll.onWall && !coll.onGround && DirToSide() /*&& !ignoreWallSlide*/) { if (x != 0 && !wallGrab) { wallSlide = true; WallSlide(); } } if (!coll.onWall || coll.onGround) { wallSlide = false; } if (coll.onWall || coll.onGround) { JumpCount = 0; //hasDashed = false; } if (Input.GetButtonDown("Jump")) { anim.SetTrigger("jump"); if (coll.onGround) { Jump(Vector2.up, false); JumpCount = 0; } else if (JumpCount == 0) { JumpCount++; Jump(Vector2.up, false); } if (coll.onWall && !coll.onGround && DirToSide()) { WallJump(); JumpCount = 0; } } if (Input.GetButtonDown("Fire1") && !hasDashed) { if (xRaw != 0 || yRaw != 0) { Dash(xRaw, yRaw); } } if (coll.onGround && !groundTouch) { GroundTouch(); groundTouch = true; } if (!coll.onGround && groundTouch) { groundTouch = false; } WallParticle(y); if (wallGrab || wallSlide || !canMove) { return; } if (x > 0) { side = 1; anim.Flip(side); } if (x < 0) { side = -1; anim.Flip(side); } }
// Update is called once per frame void Update() { if (!dd.isDeath) { float x = Input.GetAxis("Horizontal"); float y = Input.GetAxis("Vertical"); float xRaw = Input.GetAxisRaw("Horizontal"); float yRaw = Input.GetAxisRaw("Vertical"); Vector2 dir = new Vector2(xRaw, yRaw); Walk(dir); anim.SetHorizontalMovement(xRaw, yRaw, rb.velocity.y, rb.velocity.x, vertigo); if (grounded && !isDashing) { wallJumped = false; GetComponent <BetterJumping>().enabled = true; } if (Input.GetButtonDown("Jump")) { if (grounded) { Jump(Vector2.up); anim.SetTrigger("jump"); } } //DASH if (Input.GetKeyDown(KeyCode.J) && !hasDashed) { if (xRaw != 0 || yRaw != 0) { Dash(xRaw, yRaw); } } if (grounded && !groundTouch) { GroundTouch(); groundTouch = true; } if (!grounded && groundTouch) { groundTouch = false; } if (x > 0) { side = 1; anim.Flip(side); } if (x < 0) { side = -1; anim.Flip(side); } float scalay = transform.GetScaleY(); if (vertigo == true) { rb.gravityScale = -10; //transform.Rotate(new Vector3(180, 0, 0)); //sp.flipY = true; time += Time.deltaTime; if (time >= 0.5f) { transform.SetScaleY(-1 * Mathf.Abs(scalay)); time = 0; } /* if (Cam) * { * CamPlay.GetComponent<Transform>().position = new Vector3(Camo.x, Camo.y, 10f); * }*/ } else { rb.gravityScale = 10; time += Time.deltaTime; if (time >= 0.5f) { transform.SetScaleY(1 * Mathf.Abs(scalay)); time = 0; } /* if (Cam) * { * CamPlay.GetComponent<Transform>().position = new Vector3(Camo.x, Camo.y, -10f); * } */ } } }
//To preface, I am using 4 states. I considered adding jumping and falling states, but realised that because I am giving each other state a jump condition, //which varies based upon the state, and I am tracking when the player makes contact with either the ground or walls, It is safe to assume, that after jumping, // the player will be falling until they make contact with either a wall or the floor, in which event, another case will take over. So as long is the player is airborne, it is treated as Idle. // NOTE, ALSO CHANGED WALL GRAB TO X, AND DISABLED DASH RESETTING UPON GRABBING A WALL private void StateMachine(PlayerState state) { // This is where the code for each state goes switch (state) { //When player is idle, or between states, states are defaulted to IDLE case PlayerState.IDLE: //added this as a safety to ensure walk animation cannot play while idle Walk(inputDirection); anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y); //When climb is released, is not on wall, not moving if (Input.GetButtonUp("Fire2") || !coll.onWall || !canMove) { //stop grab and slide animations wallGrab = false; wallSlide = false; } //When not on wall, or is touching ground if (!coll.onWall || coll.onGround) { //stop slide and grab animations wallSlide = false; } wallGrab = false; // Condition: Horizontal input, go to RUNNING state if (xInput > 0.01f || xInput < -0.01f) { currentState = PlayerState.RUNNING; } //When is on wall, and not on ground if (coll.onWall && !coll.onGround) { //set state to WALL_SLIDING currentState = PlayerState.WALL_SLIDING; //start slide animation wallSlide = true; } //when space is pressed, jump if (Input.GetButtonDown("Jump")) { //makes sure is touching ground before jumping if (coll.onGround) { Jump(Vector2.up, false); } } //Dash when Z is pressed, and haven't dashed during this reset if (Input.GetButtonDown("Fire1") && !hasDashed) { // As long as there is some directional input if (xRaw != 0 || yRaw != 0) { // Dash using raw input values Dash(xRaw, yRaw); } } break; //Walking/Running state case PlayerState.RUNNING: // Use input direction to move and change the animation Walk(inputDirection); anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y); // Condition: No horizontal input, go to IDLE state if (xInput <= 0.01f || xInput >= 0.01f) { currentState = PlayerState.IDLE; } //When is on ground, and is not not dashing if (coll.onGround && !isDashing) { //stop wall jump animation wallJumped = false; GetComponent <BetterJumping>().enabled = true; } //when space is pressed, jump if (Input.GetButtonDown("Jump")) { if (coll.onGround) { Jump(Vector2.up, false); } } //Dash when Z is pressed, and haven't dashed during this reset if (Input.GetButtonDown("Fire1") && !hasDashed) { // As long as there is some directional input if (xRaw != 0 || yRaw != 0) { // Dash using raw input values Dash(xRaw, yRaw); } } break; //Climbing state case PlayerState.CLIMBING: //start grab animation wallGrab = true; // Stop gravity rb.gravityScale = 0; // Limit horizontal movement if (xInput > .2f || xInput < -.2f) { rb.velocity = new Vector2(rb.velocity.x, 0); } // Vertical Movement, slower when climbing float speedModifier = yInput > 0 ? .5f : 1; rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier)); //when space is pressed, wall jump if (Input.GetButtonDown("Jump")) { if (coll.onWall && !coll.onGround) { WallJump(); } wallGrab = false; } // Leave Condition: //When is not on wall or X is not held, or is on ground if (!coll.onWall || !Input.GetButton("Fire2") || coll.onGround) { // Change state to default currentState = PlayerState.IDLE; // Reset Gravity rb.gravityScale = 3; } break; //Wall sliding case case PlayerState.WALL_SLIDING: //When is on wall, and x is pressed, and can move if (coll.onWall && Input.GetButton("Fire2") && canMove) { // Change state to climbing currentState = PlayerState.CLIMBING; // Flips sprite based on which wall if (side != coll.wallSide) { anim.Flip(side * -1); } // Bools for movement and animation wallGrab = true; wallSlide = false; } //When space is pressed, and on wall, wall jump if (Input.GetButtonDown("Jump")) { //When is on wall, and is not on ground, walljump if (coll.onWall && !coll.onGround) { WallJump(); } //stop grab and slide animations wallGrab = false; wallSlide = false; } //When not moving on X, and not grabbing wall if (xInput != 0 && !wallGrab) { // Slide down the wall, start slide animation rb.gravityScale = 3; wallSlide = true; WallSlide(); } //When is not on wall, or is on ground if (!coll.onWall || coll.onGround) { //set state to default currentState = PlayerState.IDLE; //Stop slide animation wallSlide = false; } break; } }
private void StateMachine(PlayerState state) { switch (state) { case PlayerState.IDLE: if (xInput > 0.01f || xInput < -0.01f) { currentState = PlayerState.RUNNING; } break; case PlayerState.RUNNING: Walk(inputDirection); anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y); if (xInput <= 0.01f || xInput >= 0.01f) // no horizontal go to IDEL { currentState = PlayerState.IDLE; } break; case PlayerState.CLIMBING: rb.gravityScale = 0; if (xInput > .2f || xInput < -.2f) { rb.velocity = new Vector2(rb.velocity.x, 0); } float speedModifier = yInput > 0 ? .5f : 1; rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier)); //Leave condition if (!coll.onWall || !Input.GetButton("Fire2")) { currentState = PlayerState.IDLE; rb.gravityScale = 3; } break; case PlayerState.JUMPING: anim.SetTrigger("jump"); if (coll.onGround) { Jump(Vector2.up, false); } currentState = PlayerState.IDLE; break; case PlayerState.DASHING: Camera.main.transform.DOComplete(); Camera.main.transform.DOShakePosition(.2f, .5f, 14, 90, false, true); hasDashed = true; anim.SetTrigger("dash"); rb.velocity = Vector2.zero; Vector2 dir = new Vector2(xInput, yInput); rb.velocity += dir.normalized * dashSpeed; StartCoroutine(DashWait()); currentState = PlayerState.IDLE; break; case PlayerState.WALL_JUMPING: // Flip sprite if needed if ((side == 1 && coll.onRightWall) || side == -1 && !coll.onRightWall) { side *= -1; anim.Flip(side); } // Disable movement while wall jumping StopCoroutine(DisableMovement(0)); StartCoroutine(DisableMovement(.1f)); // Set direction based on which wall Vector2 wallDir = coll.onRightWall ? Vector2.left : Vector2.right; // Jump using the direction Jump((Vector2.up / 1.5f + wallDir / 1.5f), true); currentState = PlayerState.IDLE; break; case PlayerState.ON_WALL: if (coll.wallSide != side) { anim.Flip(side * -1); } if (!canMove) { return; } // If the player is holding towards the wall... bool pushingWall = false; if ((rb.velocity.x > 0 && coll.onRightWall) || (rb.velocity.x < 0 && coll.onLeftWall)) { pushingWall = true; } float push = pushingWall ? 0 : rb.velocity.x; // Move down rb.velocity = new Vector2(push, -slideSpeed); currentState = PlayerState.IDLE; break; case PlayerState.FALLING: hasDashed = false; isDashing = false; side = anim.sr.flipX ? -1 : 1; currentState = PlayerState.IDLE; break; } }
private void StateMachine(PlayerState state) { // This is where the code for each state goes switch (state) { case PlayerState.IDLE: anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y); // Condition: Horizontal input, go to RUNNING state if (xInput > 0.01f || xInput < -0.01f) { currentState = PlayerState.RUNNING; } break; case PlayerState.RUNNING: // Use input direction to move and change the animation rb.velocity = new Vector2(inputDirection.x * speed, rb.velocity.y); // Upadate animation anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y); // Condition: No horizontal input, go to IDLE state if (xInput <= 0.01f || xInput >= 0.01f) { currentState = PlayerState.IDLE; } break; case PlayerState.CLIMBING: // Stop gravity rb.gravityScale = 0; // Upadate animation anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y); // Limit horizontal movement if (xInput > .2f || xInput < -.2f) { rb.velocity = new Vector2(rb.velocity.x, 0); } // Vertical Movement, slower when climbing float speedModifier = yInput > 0 ? .5f : 1; rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier)); // Leave Condition: if (!coll.onWall || !Input.GetButton("Fire2")) { // Change state to default currentState = PlayerState.IDLE; // Reset Gravity rb.gravityScale = 3; } // Flips sprite based on which wall if (side != coll.wallSide) { anim.Flip(side * -1); } break; // More states here pls case PlayerState.ON_WALL: // Flip if needed if (coll.wallSide != side) { anim.Flip(side * -1); } if (!canMove) { return; } // If the player is holding towards the wall... bool pushingWall = false; if ((rb.velocity.x > 0 && coll.onRightWall) || (rb.velocity.x < 0 && coll.onLeftWall)) { pushingWall = true; } float push = pushingWall ? 0 : rb.velocity.x; // Move down rb.velocity = new Vector2(push, -slideSpeed); wallSlide = true; // Back to idle currentState = PlayerState.IDLE; break; case PlayerState.JUMPING: // Sets the jump animation anim.SetTrigger("jump"); // What states can you jump from? // Maybe move to IDLE and/or RUNNING if (coll.onGround) { Jump(Vector2.up, false); } // Back to idle if (xInput <= 0.01f || xInput >= 0.01f) { currentState = PlayerState.IDLE; } else { currentState = PlayerState.RUNNING; } break; case PlayerState.FALLING: groundTouch = true; hasDashed = false; isDashing = false; side = anim.sr.flipX ? -1 : 1; // Back to idle currentState = PlayerState.IDLE; break; case PlayerState.DASHING: // Graphics effects Camera.main.transform.DOComplete(); Camera.main.transform.DOShakePosition(.2f, .5f, 14, 90, false, true); FindObjectOfType <RippleEffect>().Emit(Camera.main.WorldToViewportPoint(transform.position)); // Put dash on cooldown hasDashed = true; anim.SetTrigger("dash"); rb.velocity = Vector2.zero; Vector2 dir = new Vector2(xRaw, yRaw); rb.velocity += dir.normalized * dashSpeed; StartCoroutine(DashWait()); // Back to idle currentState = PlayerState.IDLE; break; case PlayerState.WALL_JUMPING: // Flip sprite if needed if ((side == 1 && coll.onRightWall) || side == -1 && !coll.onRightWall) { side *= -1; anim.Flip(side); } // Disable movement while wall jumping StopCoroutine(DisableMovement(0)); StartCoroutine(DisableMovement(.1f)); // Set direction based on which wall Vector2 wallDir = coll.onRightWall ? Vector2.left : Vector2.right; // Jump using the direction Jump((Vector2.up / 1.5f + wallDir / 1.5f), true); // Back to idle currentState = PlayerState.IDLE; break; } }
// Update is called once per frame void Update() { float x = Input.GetAxis("Horizontal"); float y = Input.GetAxis("Vertical"); float xRaw = Input.GetAxisRaw("Horizontal"); float yRaw = Input.GetAxisRaw("Vertical"); float xpos = rb.position.x; // Player x position float ypos = rb.position.y; // Player y position float run = rb.velocity.x; // Player x velocity if (run > 7) { run = 7; } Vector2 dir = new Vector2(x, y); OSCHandler.Instance.SendMessageToClient("pd", "/unity/xpos", xpos); OSCHandler.Instance.SendMessageToClient("pd", "/unity/ypos", ypos); OSCHandler.Instance.SendMessageToClient("pd", "/unity/run", 1 - (Math.Abs(run) / 10)); // Only play walking noise if player is on the ground if (coll.onGround && !isOnGround) { OSCHandler.Instance.SendMessageToClient("pd", "/unity/grounded", "ready"); isOnGround = true; } else if (!coll.onGround && isOnGround) { OSCHandler.Instance.SendMessageToClient("pd", "/unity/grounded", "ready"); isOnGround = false; } var main = ps.main; main.simulationSpeed = particleSpeed / 40; main.startSize = (float)((particleSize - 26) * .015); Walk(dir); anim.SetHorizontalMovement(x, y, rb.velocity.y); if (coll.onWall && Input.GetButton("Fire3") && canMove) { if (side != coll.wallSide) { anim.Flip(side * -1); } wallGrab = true; wallSlide = false; } if (Input.GetButtonUp("Fire3") || !coll.onWall || !canMove) { wallGrab = false; wallSlide = false; } if (coll.onGround && !isDashing) { wallJumped = false; GetComponent <BetterJumping>().enabled = true; } if (wallGrab && !isDashing) { rb.gravityScale = 0; if (x > .2f || x < -.2f) { rb.velocity = new Vector2(rb.velocity.x, 0); } float speedModifier = y > 0 ? .5f : 1; rb.velocity = new Vector2(rb.velocity.x, y * (speed * speedModifier)); } else { rb.gravityScale = 3; } if (coll.onWall && !coll.onGround) { if (x != 0 && !wallGrab) { wallSlide = true; WallSlide(); } } if (!coll.onWall || coll.onGround) { wallSlide = false; } if (Input.GetButtonDown("Jump")) { anim.SetTrigger("jump"); if (coll.onGround) { Jump(Vector2.up, false); } if (coll.onWall && !coll.onGround) { WallJump(); } } // Play dash distortion when player dashed. if (Input.GetButtonDown("Fire1") && !hasDashed) { OSCHandler.Instance.SendMessageToClient("pd", "/unity/dash", 100); if (xRaw != 0 || yRaw != 0) { Dash(xRaw, yRaw); } } if (coll.onGround && !groundTouch) { GroundTouch(); groundTouch = true; } if (!coll.onGround && groundTouch) { groundTouch = false; } WallParticle(y); if (wallGrab || wallSlide || !canMove) { return; } if (x > 0) { side = 1; anim.Flip(side); } if (x < 0) { side = -1; anim.Flip(side); } }
private void StateMachine(PlayerState state) { // This is where the code for each state goes switch (state) { case PlayerState.IDLE: isDashing = false; // Condition: Horizontal input, go to RUNNING state if (xInput > 0.01f || xInput < -0.01f) { currentState = PlayerState.RUNNING; } //If Jump pressed jump and set state to jump if (Input.GetButtonDown("Jump")) { // Sets the jump animation anim.SetTrigger("jump"); currentState = PlayerState.JUMPING; } break; case PlayerState.RUNNING: isDashing = false; // Use input direction to move and change the animation Walk(inputDirection); anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y); // Condition: No horizontal input, go to IDLE state if (xInput <= 0.01f || xInput >= 0.01f) { currentState = PlayerState.IDLE; } //If Dash pressed jump and set state to dashing if (Input.GetButtonDown("Fire1") && !isDashing) { // As long as there is some directional input if (xRaw != 0 || yRaw != 0) { // Dash using raw input values Dash(xRaw, yRaw); isDashing = true; currentState = PlayerState.DASHING; } } //If Jump pressed jump and set state to jump if (Input.GetButtonDown("Jump")) { // Sets the jump animation anim.SetTrigger("jump"); currentState = PlayerState.JUMPING; } break; case PlayerState.CLIMBING: //isDashing = false; // Stop gravity rb.gravityScale = 0; // Limit horizontal movement if (xInput > .2f || xInput < -.2f) { rb.velocity = new Vector2(rb.velocity.x, 0); } // Vertical Movement, slower when climbing float speedModifier = yInput > 0 ? .5f : 1; rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier)); // Leave Condition: if (!coll.onWall || !Input.GetButton("Fire2")) { // Change state to default currentState = PlayerState.IDLE; // Reset Gravity rb.gravityScale = 3; } //If Jump pressed jump and set state to jump if (Input.GetButtonDown("Jump")) { // Sets the jump animation anim.SetTrigger("jump"); currentState = PlayerState.JUMPING; } break; //DASHING State case PlayerState.DASHING: //If collides with ground default to idle, sets dashing to false ^ if (coll.onGround) { // GroundTouch() resets the dash, as you can only dash once per jump isDashing = false; currentState = PlayerState.IDLE; } //if collides with wall default to climbing, climbing sets dashing to false if (coll.onWall) { isDashing = false; currentState = PlayerState.CLIMBING; } break; //Jumping state case PlayerState.JUMPING: //when jumping dashing is false //isDashing = false; //If collides with ground, default to idle and stop upward movement if (Input.GetButton("Fire1") && !isDashing) { // As long as there is some directional input if (xRaw != 0 || yRaw != 0) { // Dash using raw input values Dash(xRaw, yRaw); isDashing = true; currentState = PlayerState.DASHING; } } if (coll.onGround) { Jump(Vector2.up, false); currentState = PlayerState.IDLE; } //If collides with wall default to climbing if (coll.onWall) { Jump(Vector2.up, false); currentState = PlayerState.CLIMBING; } //If dash button pressed dash in direction and change state to dashing. MUST HAVE AN X VELOCITY OR IT WONT WORK!!! break; } }
void Update() { otherPlayer.transform.position = transform.position; if (Input.GetMouseButtonDown(1)) { otherPlayer.SetActive(true); gameObject.SetActive(false); } float x = Input.GetAxis("Horizontal"); float y = Input.GetAxis("Vertical"); float xRaw = Input.GetAxisRaw("Horizontal"); float yRaw = Input.GetAxisRaw("Vertical"); Vector2 dir = new Vector2(x, y); walk(dir); anim.SetHorizontalMovement(x, y, rb.velocity.y); if (coll.onGround && !isDashing) { wallJumped = false; GetComponent <playerJump>().enabled = true; } if (wallGrab && !isDashing) { rb.gravityScale = 0; if (x > .2f || x < -.2f) { rb.velocity = new Vector2(rb.velocity.x, 0); } float speedModifier = y > 0 ? .5f : 1; rb.velocity = new Vector2(rb.velocity.x, y * (movementSpeed * speedModifier)); } else { rb.gravityScale = 1.5f; } if (Input.GetButtonDown("Jump")) { anim.SetTrigger("jump"); if (coll.onGround) { jump(Vector2.up); } if (coll.onWall && !coll.onGround) { WallJump(); } } if (Input.GetButtonDown("Fire1") && !hasDashed) { if (xRaw != 0 || yRaw != 0) { Dash(xRaw, yRaw); } } if (coll.onWall && !coll.onGround) { if (x != 0) { wallSlide = true; WallSlide(); } } if (coll.onGround && !groundTouch) { GroundTouch(); groundTouch = true; } if (!coll.onGround && groundTouch) { groundTouch = false; } if (!coll.onWall || coll.onGround) { wallSlide = false; } if (wallSlide || !canMove) { return; } if (x > 0) { side = 1; anim.Flip(side); } if (x < 0) { side = -1; anim.Flip(side); } }
private void StateMachine(PlayerState state) { // This is where the code for each state goes switch (state) { case PlayerState.GROUNDED: GroundTouch(); // Use input direction to move and change the animation Walk(inputDirection); anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y); // When on the ground and not dashing if (!isDashing) { wallJumped = false; GetComponent <BetterJumping>().enabled = true; } if (!coll.onWall) { wallSlide = false; } if (!coll.onGround) { currentState = PlayerState.AIRBORN; } // Jump when hitting the space bar if (Input.GetButtonDown("Jump")) { currentState = PlayerState.AIRBORN; // Sets the jump animation anim.SetTrigger("jump"); Jump(Vector2.up, false); } break; case PlayerState.CLIMBING: // Stop gravity rb.gravityScale = 0; // Limit horizontal movement if (xInput > .2f || xInput < -.2f) { rb.velocity = new Vector2(rb.velocity.x, 0); } // Vertical Movement, slower when climbing float speedModifier = yInput > 0 ? .5f : 1; rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier)); // Jump when hitting the space bar if (Input.GetButtonDown("Jump")) { currentState = PlayerState.AIRBORN; // Sets the jump animation anim.SetTrigger("jump"); WallJump(); rb.gravityScale = 3; } // Leave Condition: if (!coll.onWall || !Input.GetButton("Fire2")) { // Change state to default currentState = PlayerState.AIRBORN; // Reset Gravity rb.gravityScale = 3; } if (coll.onGround) { currentState = PlayerState.GROUNDED; JumpDelay = 0; rb.gravityScale = 3; } break; case PlayerState.ONWALL: WallSlide(); // Can enter the climbing state from any state // You may want to move this depending on the states you add if (coll.onWall && Input.GetButton("Fire2") && canMove) { // Change state currentState = PlayerState.CLIMBING; // Flips sprite based on which wall if (side != coll.wallSide) { anim.Flip(side * -1); } // Bools for movement and animation wallGrab = true; wallSlide = false; } // Jump when hitting the space bar else if (Input.GetButtonDown("Jump")) { // Sets the jump animation anim.SetTrigger("jump"); WallJump(); currentState = PlayerState.AIRBORN; } if (coll.onGround) { currentState = PlayerState.GROUNDED; } break; case PlayerState.AIRBORN: JumpDelay += Time.deltaTime; Walk(inputDirection); // When on the wall and not on the gorund if (JumpDelay > 0.1f) { if (coll.onGround) { currentState = PlayerState.GROUNDED; JumpDelay = 0; } else if (coll.onWall) { // If the player is moving towards the wall if (xInput != 0 && !wallGrab) { currentState = PlayerState.ONWALL; JumpDelay = 0; // Slide down the wall wallSlide = true; } } } break; } }
private void StateMachine(PlayerState state) { // This is where the code for each state goes switch (state) { case PlayerState.IDLE: // If not on the wall and on the ground // Maybe move this to IDLE? if (!coll.onWall || coll.onGround) { wallSlide = false; } // Condition: Horizontal input, go to RUNNING state if (xInput > 0.01f || xInput < -0.01f) { currentState = PlayerState.RUNNING; } // When on the ground and not dashing // You might want to move these to a state if (coll.onGround && !isDashing) { wallJumped = false; GetComponent <BetterJumping>().enabled = true; } // If left click and if dash is not on cooldown if (Input.GetButtonDown("Fire1") && !hasDashed) { // As long as there is some directional input if (xRaw != 0 || yRaw != 0) { // Dash using raw input values Dash(xRaw, yRaw); } } // Jump when hitting the space bar if (Input.GetButtonDown("Jump")) { // What states can you jump from? // Maybe move to IDLE and/or RUNNING if (coll.onGround) { Jump(Vector2.up, false); } currentState = PlayerState.JUMPING; } // When you land on the ground if (coll.onGround && !groundTouch) { // GroundTouch() resets the dash, as you can only dash once per jump GroundTouch(); groundTouch = true; } // When you have left the ground if (!coll.onGround && groundTouch) { groundTouch = false; } break; case PlayerState.RUNNING: // Use input direction to move and change the animation Walk(inputDirection); anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y); // Condition: No horizontal input, go to IDLE state if (xInput <= 0.01f || xInput >= 0.01f) { currentState = PlayerState.IDLE; } // Jump when hitting the space bar if (Input.GetButtonDown("Jump")) { // What states can you jump from? // Maybe move to IDLE and/or RUNNING if (coll.onGround) { Jump(Vector2.up, false); } currentState = PlayerState.JUMPING; } break; case PlayerState.CLIMBING: // Stop gravity rb.gravityScale = 0; // Limit horizontal movement if (xInput > .2f || xInput < -.2f) { rb.velocity = new Vector2(rb.velocity.x, 0); } // Vertical Movement, slower when climbing float speedModifier = yInput > 0 ? .5f : 1; rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier)); // Leave Condition: if (!coll.onWall || !Input.GetButton("Fire2")) { // Change state to default currentState = PlayerState.IDLE; // Reset Gravity rb.gravityScale = 3; } break; // More states here pls case PlayerState.JUMPING: // Sets the jump animation anim.SetTrigger("jump"); currentState = PlayerState.IDLE; break; case PlayerState.ON_WALL: // Jump when hitting the space bar if (Input.GetButtonDown("Jump")) { // Maybe move to an ON_WALL state if (coll.onWall && !coll.onGround) { WallJump(); } currentState = PlayerState.JUMPING; } // Can enter the climbing state from any state // You may want to move this depending on the states you add if (coll.onWall && Input.GetButton("Fire2") && canMove) { // Change state currentState = PlayerState.CLIMBING; // Flips sprite based on which wall if (side != coll.wallSide) { anim.Flip(side * -1); } // Bools for movement and animation wallGrab = true; wallSlide = false; } // Used when no longer on a wall if (Input.GetButtonUp("Fire2") || !coll.onWall || !canMove) { wallGrab = false; wallSlide = false; } break; } }
private void StateMachine(PlayerState state) { // This is where the code for each state goes switch (state) { case PlayerState.IDLE: GetProperJump(); // Condition: Horizontal input, go to RUNNING state if (xInput > 0.01f || xInput < -0.01f) { currentState = PlayerState.RUNNING; } break; case PlayerState.RUNNING: // Use input direction to move and change the animation Walk(inputDirection); anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y); // Condition: No horizontal input, go to IDLE state if (xInput <= 0.01f || xInput >= 0.01f) { currentState = PlayerState.IDLE; } GetProperJump(); break; case PlayerState.CLIMBING: // Stop gravity rb.gravityScale = 0; // Limit horizontal movement if (xInput > .2f || xInput < -.2f) { rb.velocity = new Vector2(rb.velocity.x, 0); } // Vertical Movement, slower when climbing float speedModifier = yInput > 0 ? .5f : 1; rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier)); // Leave Condition: if (!coll.onWall || !Input.GetButton("Fire2")) { // Change state to default currentState = PlayerState.IDLE; wallGrab = false; wallSlide = false; // Reset Gravity rb.gravityScale = 3; } else if (Input.GetButtonDown("Jump")) { currentState = PlayerState.WALL_JUMPING; initiatewalljump = true; wallGrab = false; wallSlide = false; } break; case PlayerState.JUMPING: DoJump(); if (!coll.onGround) { currentState = PlayerState.FALLING; } break; case PlayerState.WALL_JUMPING: WallJump(); if (!coll.onGround && !coll.onWall) { currentState = PlayerState.FALLING; } break; case PlayerState.ON_WALL: if (initiatewalljump) { currentState = PlayerState.WALL_JUMPING; initiatewalljump = false; wallSlide = false; } else if (Input.GetButtonDown("Jump") && coll.onWall && !coll.onGround) { currentState = PlayerState.WALL_JUMPING; wallSlide = false; } else if (coll.onWall && Input.GetButton("Fire2") && canMove) { // Change state currentState = PlayerState.CLIMBING; // Flips sprite based on which wall if (side != coll.wallSide) { anim.Flip(side * -1); } // Bools for movement and animation wallGrab = true; wallSlide = false; } else if ((coll.wallSide < 0 && xInput < -0.1) || (coll.wallSide > 0 && xInput > 0.1)) { currentState = PlayerState.RUNNING; } else if (coll.onGround) { currentState = PlayerState.IDLE; wallSlide = false; } else if (!coll.onGround && !coll.onWall) { currentState = PlayerState.RUNNING; } else if (xInput != 0 && !wallGrab) { // Slide down the wall wallSlide = true; WallSlide(); } break; case PlayerState.FALLING: if (xInput <= 0.01f || xInput >= 0.01f) { Walk(inputDirection); } if (coll.onGround) { if (xInput > 0.01f || xInput < -0.01f) { currentState = PlayerState.RUNNING; } else { currentState = PlayerState.IDLE; } GroundTouch(); } else if (Input.GetButtonDown("Fire1") && !hasDashed) { // As long as there is some directional input if (xRaw != 0 || yRaw != 0) { // Dash using raw input values Dash(xRaw, yRaw); } } else if (coll.onWall && Input.GetButton("Fire2") && canMove) { // Change state currentState = PlayerState.CLIMBING; // Flips sprite based on which wall if (side != coll.wallSide) { anim.Flip(side * -1); } // Bools for movement and animation wallGrab = true; wallSlide = false; } else if (coll.onWall) { currentState = PlayerState.ON_WALL; } break; } }
private void StateMachine(PlayerState state) { // This is where the code for each state goes switch (state) { case PlayerState.IDLE: // Condition: Horizontal input, go to RUNNING state if (xInput > 0.01f || xInput < -0.01f) { currentState = PlayerState.RUNNING; } // Jump when hitting the space bar if (Input.GetButtonDown("Jump")) { // Sets the jump animation anim.SetTrigger("jump"); // What states can you jump from? // Maybe move to IDLE and/or RUNNING if (coll.onGround) { Jump(Vector2.up, false); } // Maybe move to an ON_WALL state if (coll.onWall && !coll.onGround) { WallJump(); } currentState = PlayerState.AIRBORN; } break; case PlayerState.RUNNING: // Use input direction to move and change the animation Walk(inputDirection); anim.SetHorizontalMovement(xInput, yInput, rb.velocity.y); // Condition: No horizontal input, go to IDLE state if (xInput <= 0.01f || xInput >= 0.01f) { currentState = PlayerState.IDLE; } // Jump when hitting the space bar if (Input.GetButtonDown("Jump")) { // Sets the jump animation anim.SetTrigger("jump"); // What states can you jump from? // Maybe move to IDLE and/or RUNNING if (coll.onGround) { Jump(Vector2.up, false); } // Maybe move to an ON_WALL state if (coll.onWall && !coll.onGround) { WallJump(); } currentState = PlayerState.AIRBORN; } // If not on the wall and on the ground // Maybe move this to IDLE? if (!coll.onWall || coll.onGround) { wallSlide = false; } break; case PlayerState.CLIMBING: // Stop gravity rb.gravityScale = 0; // Limit horizontal movement if (xInput > .2f || xInput < -.2f) { rb.velocity = new Vector2(rb.velocity.x, 0); } // Vertical Movement, slower when climbing float speedModifier = yInput > 0 ? .5f : 1; rb.velocity = new Vector2(rb.velocity.x, yInput * (speed * speedModifier)); // Leave Condition: if (!coll.onWall || !Input.GetButton("Fire2")) { // Change state to default currentState = PlayerState.IDLE; // Reset Gravity rb.gravityScale = 3; } break; case PlayerState.GROUNDED: GroundTouch(); groundTouch = true; // When on the ground and not dashing // You might want to move these to a state if (coll.onGround && !isDashing) { wallJumped = false; GetComponent <BetterJumping>().enabled = true; } currentState = PlayerState.IDLE; break; case PlayerState.AIRBORN: // When you have left the ground groundTouch = false; // When you land on the ground if (coll.onGround) { currentState = PlayerState.GROUNDED; } // When on the wall and not on the gorund if (coll.onWall && !coll.onGround) { currentState = PlayerState.ON_WALL; } break; case PlayerState.ON_WALL: // If the player is moving towards the wall if (xInput != 0 && !wallGrab) { // Slide down the wall wallSlide = true; WallSlide(); } // Used when no longer on a wall if (Input.GetButtonUp("Fire2") || !coll.onWall || !canMove) { wallGrab = false; wallSlide = false; } // Jump when hitting the space bar if (Input.GetButtonDown("Jump")) { // Sets the jump animation anim.SetTrigger("jump"); // Maybe move to an ON_WALL state if (coll.onWall && !coll.onGround) { WallJump(); } currentState = PlayerState.AIRBORN; } break; // More states here pls } }