void Update() { // If the two speed vars can't be used to calc the lulled position, then exit if (horizLaxSpeed <= 0 || vertLaxSpeed <= 0) { return; } try { // If the player is in the level, then follow the camera with a lag // If not, the reset the sprite to where it was before if (Camera.main.GetComponent <CamHandler> ().currentLevelSprite.name == parentLevelName + "-foreground") { desiredPos = new Vector3((Camera.main.transform.position.x / horizLaxSpeed), (Camera.main.transform.position.y / vertLaxSpeed), transform.position.z); // Find the camera, translate to position & divide by parallax speed desiredPos.x += xOffset; desiredPos.y += yOffset; transform.position = desiredPos; PixelSnapper.SnapToNearestPixel(this.gameObject); } else { desiredPos = new Vector3(0, 0, 0); transform.position = startPos; } } catch { } }
// FixedUpdate is called once per frame // This function plays nicer with RigidBody2D private void FixedUpdate() { if (GameManager.gameIsPaused) { return; } // See if the player is touching the ground CheckGrounded(); // See if the player is touching a wall & if so, which side? CheckTouchingWall(); if (grounded || wallSlidingL || wallSlidingR || climbingLadder || climbingRope) { canJump = true; if (playerCostume == Costume.AGILE) { canJumpAgain = true; } } else { canJump = false; } // ** PHYSICS ** // Ground Movement // Applying the velocity + drag to the RigidBody2D if (movementSpeed > 0 && grounded) { movementSpeed += -groundDrag; myRigidBody2D.velocity = PixelSnapper.SnapToNearestPixel(new Vector2(movementSpeed, myRigidBody2D.velocity.y)); } else if (movementSpeed > 0 && !grounded) { movementSpeed += -airDrag; myRigidBody2D.velocity = PixelSnapper.SnapToNearestPixel(new Vector2(movementSpeed, myRigidBody2D.velocity.y)); } else if (movementSpeed < 0 && grounded) { movementSpeed += groundDrag; myRigidBody2D.velocity = PixelSnapper.SnapToNearestPixel(new Vector2(movementSpeed, myRigidBody2D.velocity.y)); } else if (movementSpeed < 0 && !grounded) { movementSpeed += airDrag; myRigidBody2D.velocity = PixelSnapper.SnapToNearestPixel(new Vector2(movementSpeed, myRigidBody2D.velocity.y)); } else if (movementSpeed == 0 && grounded) { myRigidBody2D.velocity = PixelSnapper.SnapToNearestPixel(new Vector2(0, myRigidBody2D.velocity.y)); } // Equalizing the player movement to 0 if they are standing still // Fixes the 'sub-increment' problem with the calculation of movementSpeed if ((Mathf.Abs(movementSpeed) > 0 && Mathf.Abs(movementSpeed) < 0.5)) { movementSpeed = 0; } // Jumping // Raise the player while they hold the jump button if (jumpPress) { if (!wallSlidingL && !wallSlidingR) // Normal jump { myRigidBody2D.velocity = PixelSnapper.SnapToNearestPixel(new Vector2(myRigidBody2D.velocity.x, maxJump)); jumpPress = false; } else if (wallSlidingL) // Wall jumps //StartCoroutine(BlockInput(.05f)); { faceDir = Face.RIGHT; touchingWallL = false; movementSpeed += walljumpForce; myRigidBody2D.velocity = PixelSnapper.SnapToNearestPixel(new Vector2(movementSpeed, maxJump)); //myRigidBody2D.AddForce(new Vector2(walljumpForce * 1.25f, walljumpForce * 2.5f)); jumpPress = false; } else if (wallSlidingR) { //StartCoroutine(BlockInput(.05f)); faceDir = Face.LEFT; touchingWallR = false; movementSpeed += -walljumpForce; myRigidBody2D.velocity = PixelSnapper.SnapToNearestPixel(new Vector2(movementSpeed, maxJump)); //myRigidBody2D.AddForce(new Vector2(-walljumpForce * 1.25f, walljumpForce * 2.5f)); jumpPress = false; } } // Drop the player if they let go of the jump button if (jumpCancel) { if (myRigidBody2D.velocity.y > minJump) { myRigidBody2D.velocity = PixelSnapper.SnapToNearestPixel(new Vector2(myRigidBody2D.velocity.x, minJump)); } jumpCancel = false; } // Climbing if (climbingUpward) { transform.position = PixelSnapper.SnapToNearestPixel(new Vector3(transform.position.x, transform.position.y + climbSpeed, transform.position.z)); } if (climbingDownward) { transform.position = PixelSnapper.SnapToNearestPixel(new Vector3(transform.position.x, transform.position.y - climbSpeed, transform.position.z)); } // Flip the sprite to change the direction the player is facing FlipPlayer(faceDir); // Check if the player's horizontal velocity is other than 0 & they're pressing the movement buttons // to see if they are deliberately moving if ((Mathf.Abs(myRigidBody2D.velocity.x) > 0) && (InputManager.LeftButton() || InputManager.RightButton())) { moving = true; } else { moving = false; } if ((climbingRope || climbingLadder) && climbableObj != null) { transform.position = PixelSnapper.SnapToNearestPixel(new Vector3(climbableObj.transform.position.x, transform.position.y, transform.position.z)); myRigidBody2D.gravityScale = 0f; myRigidBody2D.velocity = new Vector2(0, 0); movementSpeed = 0; return; } // Change the player's RigidBody2D gravity scale while sliding down wall if ((wallSlidingL || wallSlidingR) && !grounded) { myRigidBody2D.gravityScale = regularGravityScale * slidingGravityMultiplier; if (touchingWallL) { faceDir = Face.RIGHT; } else if (touchingWallR) { faceDir = Face.LEFT; } } else { myRigidBody2D.gravityScale = regularGravityScale; } // Lower gravity while they are rising in the air if (rising && !(climbingRope || climbingLadder)) { myRigidBody2D.gravityScale = regularGravityScale * risingJumpMultiplier; } if (touchingWallL || touchingWallR) { movementSpeed = 0f; } }