protected void FixedUpdate() { Debug.Log("entrou no Update da bagaça"); bool grounded = groundDown.DoRaycast(transform.position); if (!grounded) { velocity.y -= gravity * Time.deltaTime; } else { velocity.y = 0; } Vector2 displacement = Vector2.zero; if (velocity.x > 0) { displacement.x = moveRight.DoRaycast(transform.position, velocity.x * Time.deltaTime); } else if (velocity.x < 0) { displacement.x = -moveLeft.DoRaycast(transform.position, -velocity.x * Time.deltaTime); } if (velocity.y > 0) { displacement.y = moveRight.DoRaycast(transform.position, velocity.y * Time.deltaTime); } else if (velocity.y < 0) { displacement.y = -moveLeft.DoRaycast(transform.position, -velocity.y * Time.deltaTime); } transform.Translate(displacement); }
private void FixedUpdate() { if (pausePlayer) { Collider2D standingOn = groundDown.DoRaycast(transform.position); grounded = standingOn != null; switch (jumpState) { case JumpState.None: if (grounded && jumpStartTimer >= 0) { jumpStartTimer = 0; jumpState = JumpState.Holding; jumpHoldTimer = 0; velocity.y = jumpStartSpeed; } break; case JumpState.Holding: jumpHoldTimer += Time.deltaTime; if (jumpInputDown == false || jumpHoldTimer >= jumpMaxHoldPeriod) { jumpState = JumpState.None; velocity.y = Mathf.Lerp(jumpMinSpeed, jumpStartSpeed, jumpHoldTimer / jumpMaxHoldPeriod); } break; } float horizInput = Input.GetAxisRaw("Horizontal"); if (horizInput < 0) { sr.flipX = true; } else if (horizInput > 0) { sr.flipX = false; } int wantedDirection = GetSign(horizInput); int velocityDirection = GetSign(velocity.x); if (wantedDirection != 0) { if (wantedDirection != velocityDirection) { velocity.x = horizSnapSpeed * wantedDirection; } else { velocity.x = Mathf.MoveTowards(velocity.x, horizMaxSpeed * wantedDirection, horizSpeedUpAccel * Time.deltaTime); } } else { velocity.x = Mathf.MoveTowards(velocity.x, 0, horizSpeedDownAccel * Time.deltaTime); } if (jumpState == JumpState.None) { velocity.y -= gravity * Time.deltaTime; } Vector2 displacement = Vector2.zero; Vector2 wantedDispl = velocity * Time.deltaTime; if (standingOn != null) { if (lastStandingOn == standingOn) { lastStandingOnVel = (Vector2)standingOn.transform.position - lastStandingOnPos; wantedDispl += lastStandingOnVel; } else if (standingOn == null) { velocity += lastStandingOnVel / Time.deltaTime; wantedDispl += lastStandingOnVel; } lastStandingOnPos = standingOn.transform.position; } lastStandingOn = standingOn; if (wantedDispl.x > 0) { displacement.x = moveRight.DoRaycast(transform.position, wantedDispl.x); } else if (wantedDispl.x < 0) { displacement.x = -moveLeft.DoRaycast(transform.position, -wantedDispl.x); } if (wantedDispl.y > 0) { displacement.y = moveUp.DoRaycast(transform.position, wantedDispl.y); } else if (wantedDispl.y < 0) { displacement.y = -moveDown.DoRaycast(transform.position, -wantedDispl.y); } if (Mathf.Approximately(displacement.x, wantedDispl.x) == false) { velocity.x = 0; } if (Mathf.Approximately(displacement.y, wantedDispl.y) == false) { velocity.y = 0; } transform.Translate(displacement); } }
//This function translates 2 ints to movement of the object and it will switch between 4 directions in which 2 rays are raycasting public void Movement(int wantedDir, int velocityDir) { int wantedDirection = wantedDir; int velocityDirection = velocityDir; if (wantedDirection != 0) { if (wantedDirection != velocityDirection) { velocity.x = horizSnapSpeed * wantedDirection; } else if (smooth == true) { velocity.x = Mathf.MoveTowards(velocity.x, horizMaxSpeed * wantedDirection, horizSpeedUpAccel * Time.deltaTime); } else if (smooth == false) { velocity.x = horizMaxSpeed * wantedDirection; } } else if (smooth == true) { velocity.x = Mathf.MoveTowards(velocity.x, 0, horizSpeedDownAccel * Time.deltaTime); } else if (smooth == false) { velocity.x = 0; } velocity.y -= gravity * Time.deltaTime; displacement = Vector2.zero; Vector2 wantedDispl = velocity * Time.deltaTime; if (velocity.x > 0) { displacement.x = moveRight.DoRaycast(transform.position, wantedDispl.x); } else if (velocity.x < 0) { displacement.x = -moveLeft.DoRaycast(transform.position, -wantedDispl.x); } if (velocity.y > 0) { displacement.y = moveUp.DoRaycast(transform.position, wantedDispl.y); } else if (velocity.y < 0) { displacement.y = -moveDown.DoRaycast(transform.position, -wantedDispl.y); } transform.Translate(displacement); if (Mathf.Approximately(displacement.x, wantedDispl.x) == false) { velocity.x = 0; } if (Mathf.Approximately(displacement.y, wantedDispl.y) == false) { velocity.y = 0; } }
private void FixedUpdate() { Collider2D standingOn = groundDown.DoRaycast(transform.position); bool grounded = standingOn != null; if (grounded && lastGrounded == false) { audioManager.fx.PlayOneShot(landFX, 0.25f); } lastGrounded = grounded; switch (jumpState) { case JumpState.None: /*if(grounded && jumpStartTimer > 0) { * jumpStartTimer = 0; * jumpState = JumpState.Holding; * jumpHoldTimer = 0; * velocity.y = jumpStartSpeed; * jumpSFX.Play(); * }*/ if (!stunned) { if (jumpStartTimer > 0) { jumpStartTimer = 0; jumpState = JumpState.Holding; jumpHoldTimer = 0; velocity.y = jumpStartSpeed; //jumpSFX.Play(); } } break; case JumpState.Holding: jumpHoldTimer += Time.deltaTime; if (jumpInputDown == false || jumpHoldTimer >= jumpMaxHoldPeriod) { jumpState = JumpState.None; velocity.y = Mathf.Lerp(jumpMinSpeed, jumpStartSpeed, jumpHoldTimer / jumpMaxHoldPeriod); // Lerp! //float p = jumpHoldTimer / jumpMaxHoldPeriod; //velocity.y = jumpMinSpeed + (jumpStartSpeed - jumpMinSpeed) * p; } break; } switch (dashState) { case DashState.None: if (!stunned) { if (dashStartTimer > 0) { dashStartTimer = 0; dashState = DashState.Holding; dashHoldTimer = 0; //dashFX.Play(); } } break; case DashState.Holding: dashHoldTimer += Time.deltaTime; if (dashInputDown == false || dashHoldTimer >= dashMaxHoldPeriod) { dashState = DashState.None; } break; } switch (boostState) { case BoostState.None: if (!stunned) { if (boostStartTimer > 0) { boostStartTimer = 0; boostState = BoostState.Holding; boostHoldTimer = 0; //boostFX.Play(); } } break; case BoostState.Holding: boostHoldTimer += Time.deltaTime; if (boostInputDown == false || boostHoldTimer >= boostMaxHoldPeriod) { boostState = BoostState.None; } break; } switch (eraseState) { case EraseState.None: if (!stunned) { if (eraseStartTimer > 0) { eraseStartTimer = 0; eraseState = EraseState.Holding; erasehHoldTimer = 0; //eraseFX.Play(); } } break; case EraseState.Holding: erasehHoldTimer += Time.deltaTime; if (eraseInputDown == false || erasehHoldTimer >= eraseMaxHoldPeriod) { eraseState = EraseState.None; } break; } switch (crouchState) { case CrouchState.None: if (grounded && !stunned) { if (crouchStartTimer > 0) { crouchStartTimer = 0; crouchState = CrouchState.Holding; crouchHoldTimer = 0; boxCollider.enabled = false; } if (dropPlatform != null) { dropPlatform.enabled = true; } } break; case CrouchState.Holding: crouchHoldTimer += Time.deltaTime; dropPlatform = lastStandingOn; if (dropPlatform.gameObject.tag == "Droppable") { if (crouchInputDown == false || stunned) { crouchState = CrouchState.None; boxCollider.enabled = true; } else if (crouchHoldTimer >= crouchDropPeriod) { crouchState = CrouchState.None; boxCollider.enabled = true; lastStandingOn.enabled = false; } } else { if (crouchInputDown == false || stunned) { crouchState = CrouchState.None; boxCollider.enabled = true; } } break; } float horizInput = Input.GetAxisRaw("Horizontal"); int wantedDirection = GetSign(horizInput); int velocityDirection = GetSign(velocity.x); if (wantedDirection != 0) { if (wantedDirection != velocityDirection) { if (boost <= 0) { velocity.x = horizSnapSpeed * wantedDirection; //audioManager.fx.PlayOneShot(startMoveFX, 0.3f); } else { if (dashState == DashState.Holding) { dash += (int)dashSpeed; if (dash < 32) { if (!spriteRenderer.flipX) { velocity.x = dash; } else if (spriteRenderer.flipX) { velocity.x = -dash; } } else { velocity.x = Mathf.MoveTowards(velocity.x, 0, 75 * Time.deltaTime); } } else { velocity.x = 0; } } } else { if (!stunned) { if (dashState == DashState.Holding) { dash += (int)dashSpeed; if (dash < 32) { if (!spriteRenderer.flipX) { velocity.x = dash; } else if (spriteRenderer.flipX) { velocity.x = -dash; } } else { velocity.x = Mathf.MoveTowards(velocity.x, 0, 75 * Time.deltaTime); } } else if (boostState == BoostState.Holding) { boost += (int)boostSpeed; velocity.x = 0; if (boost <= 36) { velocity.y = boost; } else { velocity.y = Mathf.MoveTowards(velocity.y, 0, 125 * Time.deltaTime); } } else { if (boost <= 0) { dash = 0; boost = 0; velocity.x = Mathf.MoveTowards(velocity.x, horizMaxSpeed * wantedDirection, horizSpeedUpAccel * Time.deltaTime); } else { velocity.x = 0; velocity.y = Mathf.MoveTowards(velocity.y, 0, 200 * Time.deltaTime); } } } } } else { if (dashState == DashState.Holding) { dash += (int)dashSpeed; if (dash < 32) { if (!spriteRenderer.flipX) { velocity.x = dash; } else if (spriteRenderer.flipX) { velocity.x = -dash; } } else { velocity.x = Mathf.MoveTowards(velocity.x, 0, 75 * Time.deltaTime); } } else if (boostState == BoostState.Holding) { boost += (int)boostSpeed; velocity.x = 0; if (boost <= 36) { velocity.y = boost; } else { velocity.y = Mathf.MoveTowards(velocity.y, 0, 125 * Time.deltaTime); } } else { if (dash > 0) { dash -= (int)dashSpeed; velocity.x = Mathf.MoveTowards(velocity.x, 0, 150 * Time.deltaTime); } else { if (boost > 0) { velocity.x = 0; velocity.y = Mathf.MoveTowards(velocity.y, 0, 200 * Time.deltaTime); } else { dash = 0; boost = 0; velocity.x = Mathf.MoveTowards(velocity.x, 0, horizSpeedDownAccel * Time.deltaTime); } } } } if (dashState != DashState.Holding) { if (jumpState == JumpState.None) { if (boostState != BoostState.Holding) { if (eraseState != EraseState.Holding) { if (boost > 0) { boost -= (int)boostSpeed / 2; velocity.y -= gravity * Time.deltaTime; velocity.x = 0; } else { velocity.y -= gravity * Time.deltaTime; if (crouchState == CrouchState.Holding) { velocity.x = 0; } } } else { velocity.x = 0; velocity.y = 0; } } else if (boostState == BoostState.Holding) { velocity.x = 0; stunned = false; } } if (stunned) { if (currentX != 0) { velocity.x = (currentX * -1) * stunBounce; stunTimer += Time.deltaTime; } else { velocity.x = (enemyX * -3) * stunBounce; stunTimer += Time.deltaTime; } if (stunTimer >= stunPeriod) { stunned = false; velocity.x = 0; } } else { currentX = velocity.x; stunTimer = 0; } } else if (dashState == DashState.Holding) { velocity.y += vertBoost * Time.deltaTime; stunned = false; } Vector2 displacement = Vector2.zero; Vector2 wantedDispl = velocity * Time.deltaTime; if (standingOn != null) { if (lastStandingOn == standingOn) { lastStandingOnVel = (Vector2)standingOn.transform.position - lastStandingOnPos; wantedDispl += lastStandingOnVel; } else if (standingOn == null) { velocity += lastStandingOnVel / Time.deltaTime; wantedDispl += lastStandingOnVel; } lastStandingOnPos = standingOn.transform.position; } lastStandingOn = standingOn; if (wantedDispl.x > 0) { displacement.x = moveRight.DoRaycast(transform.position, wantedDispl.x); } else if (wantedDispl.x < 0) { displacement.x = -moveLeft.DoRaycast(transform.position, -wantedDispl.x); } if (wantedDispl.y > 0) { displacement.y = moveUp.DoRaycast(transform.position, wantedDispl.y); } else if (wantedDispl.y < 0) { displacement.y = -moveDown.DoRaycast(transform.position, -wantedDispl.y); } if (Mathf.Approximately(displacement.x, wantedDispl.x) == false) { velocity.x = 0; } if (Mathf.Approximately(displacement.y, wantedDispl.y) == false) { velocity.y = 0; } transform.Translate(displacement); if (!stunned) { if (dashState == DashState.Holding) { if (playerAnim.cycleCounter != 4) { playerAnim.flip = true; playerAnim.cycleCounter = 4; } } else { if (jumpState == JumpState.Holding) { if (playerAnim.cycleCounter != 3) { playerAnim.flip = true; playerAnim.cycleCounter = 3; } } else { if (eraseState == EraseState.Holding) { if (playerAnim.cycleCounter != 8) { playerAnim.flip = true; playerAnim.cycleCounter = 8; } } else { if (grounded) { if (wantedDirection == 0) { if (crouchState == CrouchState.Holding) { if (playerAnim.cycleCounter != 6) { playerAnim.flip = true; playerAnim.cycleCounter = 6; } } else { if (playerAnim.cycleCounter != 0) { playerAnim.flip = true; playerAnim.cycleCounter = 0; } } } else { if (playerAnim.cycleCounter != 1) { playerAnim.flip = true; playerAnim.cycleCounter = 1; } } } else { if (velocity.y < 0) { if (playerAnim.cycleCounter != 2) { playerAnim.flip = true; playerAnim.cycleCounter = 2; } } else { if (boostState == BoostState.Holding) { if (playerAnim.cycleCounter != 5) { playerAnim.flip = true; playerAnim.cycleCounter = 5; } } } } } } } } else if (stunned) { if (playerAnim.cycleCounter != 7) { playerAnim.flip = true; playerAnim.cycleCounter = 7; } } if (wantedDirection != 0) { spriteRenderer.flipX = wantedDirection < 0; blankSR.flipX = wantedDirection < 0; } }
private void Update() { //else //{ trail.emitting = false; particles.SetActive(false); jumpStartTimer -= Time.deltaTime; bool jumpBtn = Input.GetButton("Jump"); if (jumpBtn && jumpInputDown == false) { jumpStartTimer = CoyoteTime; } //Koden för wallclinging är helt min egen, här kollar den om det finns en vägg till höger eller vänster. if (wallRight.RaycastCheckClimbable(transform.position) == true) { climbDirection = 1; } else if (wallLeft.RaycastCheckClimbable(transform.position) == true) { climbDirection = -1; } else { climbDirection = 0; } jumpInputDown = jumpBtn; Position.x = transform.position.x; Position.y = transform.position.y; //} //private void FixedUpdate() ////{ //animator.SetFloat("Speed", velocity.magnitude); //facingRight = playerSprite.flipX; switch (jumpState) { case JumpState.None: //JumpState none är alltså när man inte hoppar //den byter till "Holding" när spelaren klickar på den angivna knappen och har jumps kvar if (jumps > 0 && jumpStartTimer > 0) { //jumps = jumps - 1; jumpStartTimer = 0; jumpState = JumpState.Holding; jumpHoldTimer = 0; //Då ökas även den verikala velocityn. velocity.y = jumpStartSpeed; } break; case JumpState.Holding: //JumpState Holding stängs av om man släpper knappen eller efter att man har hållt inne för länge. jumpHoldTimer += Time.deltaTime; //animator.SetBool("IsJumping", true); if (jumpInputDown == false || jumpHoldTimer >= jumpMaxHoldPeriod) { jumps = jumps - 1; jumpState = JumpState.None; velocity.y = Mathf.Lerp(jumpMinSpeed, jumpStartSpeed, jumpHoldTimer / jumpMaxHoldPeriod); } break; } if (groundDown.DoRaycast(transform.position) == true) { //animator.SetBool("IsJumping", false); jumps = extraJumps + 1; } else { } float xInput = Input.GetAxisRaw("Horizontal"); int wantedDirection = GetSign(xInput); int velocityDirection = GetSign(velocity.x); moveInput = wantedDirection; if (facingRight == false && moveInput > 0) //this part is for flipping the player sprite depending on what direction its moving { Flip(); //call flip method } else if (facingRight == true && moveInput < 0) { Flip(); } if (wantedDirection != 0) { //animator.SetFloat("Speed", 1); if (wantedDirection != velocityDirection) { velocity.x = xSnapSpeed * wantedDirection; } else { velocity.x = Mathf.MoveTowards(velocity.x, xMaxSpeed * wantedDirection, xSpeedUpAccel * Time.deltaTime); } } else { //animator.SetFloat("Speed", 0); velocity.x = Mathf.MoveTowards(velocity.x, 0, xSpeedDownAccel * Time.deltaTime); } switch (clingState) { case ClingState.None: if (climbDirection == wantedDirection && climbDirection != 0 && groundDown.DoRaycast(transform.position) != true) { clingState = ClingState.Holding; jumps = extraJumps + 1; } break; case ClingState.Holding: jumpState = JumpState.None; velocity.y = 0f; if (wantedDirection == 0 && climbDirection != 0) { clingState = ClingState.Sliding; } else if (climbDirection == 1 && wantedDirection < 0 || climbDirection == -1 && wantedDirection > 0) { clingState = ClingState.None; } break; case ClingState.Sliding: velocity.y -= wallSlideAccel * Time.deltaTime; if (velocity.y >= gravity || wantedDirection == climbDirection * -1) { clingState = ClingState.None; } else if (climbDirection == wantedDirection && climbDirection != 0) { clingState = ClingState.Holding; jumps = extraJumps + 1; } break; } if (jumpState == JumpState.None && clingState == ClingState.None && isPogoing == false) { velocity.y -= gravity * Time.deltaTime; } Vector2 displacement = Vector2.zero; Vector2 wantedDisplacement = velocity * Time.deltaTime; if (velocity.x > 0) { displacement.x = moveRight.DoRaycast(transform.position, wantedDisplacement.x); } else if (velocity.x < 0) { displacement.x = -moveLeft.DoRaycast(transform.position, -wantedDisplacement.x); } if (velocity.y > 0) { displacement.y = moveUp.DoRaycast(transform.position, wantedDisplacement.y); } else if (velocity.y < 0) { displacement.y = -moveDown.DoRaycast(transform.position, -wantedDisplacement.y); } if (Mathf.Approximately(displacement.x, wantedDisplacement.x) == false) { velocity.x = 0; } if (Mathf.Approximately(displacement.y, wantedDisplacement.y) == false) { velocity.y = 0; } transform.Translate(displacement); //} switch (dashState) { case DashState.None: if (dashCooldownLeft >= 0) { dashCooldownLeft -= Time.deltaTime; //foreach (var item in outlineShades) //{ // item.color = new Color(0, 0, 0, 0); //} } else { foreach (var item in outlineShades) { item.color = new Color(0, 0, 0, 255); } } if (Input.GetMouseButtonDown(0) && dashCooldownLeft < 0) { hurtBox.enabled = false; hitBox.enabled = true; foreach (var item in outlineShades) { item.color = new Color(0, 0, 0, 0); } particles.SetActive(true); if (facingRight && transform.position.x - dashPosition.x < 0) { Flip(); } if (!facingRight && transform.position.x - dashPosition.x > 0) { Flip(); } velocity = Vector3.zero; dashCooldownLeft = dashCooldown; dashPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); dashPosition.z = 0; dashTimeLeft = dashTime; dashState = DashState.Dashing; } break; case DashState.Dashing: if (dashTimeLeft >= 0) { transform.position = Vector3.Lerp(transform.position, dashPosition, dashTimeLeft); dashTimeLeft -= Time.deltaTime; trail.emitting = true; } else { hitBox.enabled = false; hurtBox.enabled = true; if (killEnemies.enemiesToMurder != null) { killEnemies.KillEnemies(); } dashState = DashState.None; } break; } }
void FixedUpdate() { grounded = groundDown.DoRaycast(transform.position); float horizontalInput = Input.GetAxisRaw("Horizontal"); int wantedDirection = GetSign(horizontalInput); int velocityDirection = GetSign(velocity.x); switch (jumpState) { case JumpState.None: if (grounded && jumpStartTimer >= 0) { jumpStartTimer = 0; jumpState = JumpState.Holding; jumpHoldTimer = 0; velocity.y = jumpStartSpeed; } else if (!grounded) { if (doubleJump) { if (jumpHit && extraJumps > 0) { jumpStartTimer = 0; jumpState = JumpState.Holding; jumpHoldTimer = 0; velocity.y = doubleJumpStartSpeed; extraJumps--; } } } break; case JumpState.Holding: jumpHoldTimer += Time.deltaTime; if (!jumpInputDown || jumpHoldTimer >= jumpMaxHoldPeriod) { jumpState = JumpState.None; jumpHit = false; // lerp! // float p = jumpHoldTimer / jumpMaxHoldPeriod; // velocity.y = jumpMinSpeed + (jumpStartSpeed - jumpMinSpeed) * p; velocity.y = Mathf.Lerp(jumpMinSpeed, jumpStartSpeed, jumpHoldTimer / jumpMaxHoldPeriod); } break; } if (jumpState == JumpState.None) { velocity.y -= gravity * Time.deltaTime; } if (wantedDirection != 0) { if (wantedDirection != velocityDirection) { velocity.x = horizontalStartUpSpeed * wantedDirection; } else { velocity.x = Mathf.MoveTowards(velocity.x, horizontalMaxSpeed * wantedDirection, horizontalSpeedUpAccel * Time.deltaTime); } } else { velocity.x = Mathf.MoveTowards(velocity.x, 0, horizontalSpeedDownAccel * Time.deltaTime); } if (!Mathf.Approximately(horizontalInput, 0)) { velocity.x = Mathf.MoveTowards(velocity.x, horizontalMaxSpeed * Mathf.Sign(horizontalInput), horizontalSpeedUpAccel * Time.deltaTime); } else { velocity.x = Mathf.MoveTowards(velocity.x, 0, horizontalSpeedDownAccel * Time.deltaTime); } if (!grounded) { ySpeed = velocity.y; animator.SetFloat("ySpeed", ySpeed); animator.SetBool("isGrounded", false); if (ySpeed > 0.01f) { animator.SetBool("isGrounded", false); animator.SetBool("isJumping", true); } else { animator.SetBool("isJumping", false); } } else { animator.SetBool("isGrounded", true); animator.SetBool("isJumping", false); xSpeed = velocity.x; animator.SetFloat("xSpeed", xSpeed); if (xSpeed != 0) { animator.SetBool("isWalking", true); } else { animator.SetBool("isWalking", false); } } Vector2 displacement = Vector2.zero; Vector2 wantedDisplacement = velocity * Time.deltaTime; if (velocity.x > 0) { displacement.x = moveRight.DoRaycast(transform.position, wantedDisplacement.x); } else if (velocity.x < 0) { displacement.x = -moveLeft.DoRaycast(transform.position, -wantedDisplacement.x); } if (velocity.y > 0) { displacement.y = moveUp.DoRaycast(transform.position, wantedDisplacement.y); } else if (velocity.y < 0) { displacement.y = -moveDown.DoRaycast(transform.position, -wantedDisplacement.y); } if (!Mathf.Approximately(displacement.x, wantedDisplacement.x)) { velocity.x = 0; } if (!Mathf.Approximately(displacement.y, wantedDisplacement.y)) { velocity.y = 0; } transform.Translate(displacement); // if(wantedDirection != 0) { // spriteRenderer.flipX = wantedDirection < 0; // } if (horizontalInput > 0 && !facingRight) { Flip(); } else if (horizontalInput < 0 && facingRight) { Flip(); } }