public IEnumerator MoveToPosition(bool moveRight, float timeToMove) { moving = true; Vector3 position = transform.position; var currentPos = transform.position; if (moveRight) { position.x += 1; } else { position.x -= 1; } var t = 0f; while (t < 1) { t += Time.deltaTime / timeToMove; float roundedTime = PixelRounder.RoundByPixels(t, 32); transform.position = Vector3.Lerp(currentPos, position, roundedTime); yield return(null); } transform.position = position; moving = false; }
// Update is called once per frame void FixedUpdate() { RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x - 0.499f, transform.position.y - 0.525f), Vector2.right, 0.998f, isObject); if (hit.collider != null) { if (hit.collider.tag == "Player") { hit.collider.GetComponent <PlayerController>().isDead = true; } else { grounded = true; velocity = 2; } } else { grounded = false; float newPos = pos - (velocity * Time.fixedDeltaTime); RaycastHit2D[] hitArray = Physics2D.RaycastAll(new Vector2(transform.position.x, pos - 0.5f), Vector2.down, pos - newPos, isObject); foreach (RaycastHit2D downHit in hitArray) { if (downHit.collider != null && downHit.collider.gameObject != this.gameObject) { newPos = pos - downHit.distance + 0.001f; break; } } pos = newPos; transform.position = new Vector3(transform.position.x, PixelRounder.RoundByPixels(pos, 32), 0); } }
// Update is called once per frame void FixedUpdate() { currentPushRecovery += Time.fixedDeltaTime; if (currentPushRecovery > pushRecovery) { if (Input.GetKeyDown(KeyCode.P)) { HitCrate(false); } else if (Input.GetKeyDown(KeyCode.L)) { HitCrate(true); } } #region Check feet collision and jumping //Check if the player is standing on the ground RaycastHit2D feetHit = Physics2D.Raycast(new Vector2(transform.position.x - 0.374f, transform.position.y + -0.751f), Vector2.right, 0.748f, groundSurfaces); if (feetHit.collider != null && feetHit.collider.tag == "Ground") { grounded = true; verticalVelocity = 0; int height = (int)Mathf.Round(transform.position.y); if (height > maxHeight) { maxHeight = height; } } else { grounded = false; } //Check if user pressed the space bar and is on the ground if (grounded) { if (Input.GetKey(KeyCode.Space)) { verticalVelocity = jumpForce; } } else { verticalVelocity -= 9.80665f * mass * Time.fixedDeltaTime; } #endregion #region horizontal movement //Check if user is pressing A or D float horizontalMovement = Input.GetAxisRaw("Horizontal"); if (horizontalMovement != 0) { if (horizontalMovement == 1) { if (!facingRight) { facingRight = true; Flip(); } } else { if (facingRight) { facingRight = false; Flip(); } } float newVelocity = horizontalMovement * horizontalAcceleration * Time.fixedDeltaTime; if (horizontalVelocity < 0 && horizontalMovement == 1 || horizontalVelocity > 0 && horizontalMovement == -1) { newVelocity *= turnMultiplier; } //Add velocity horizontalVelocity += newVelocity; //If velocity is bigger than max velocity, limit it and set it to the max if (horizontalVelocity > horizontalMaxVelocity) { horizontalVelocity = horizontalMaxVelocity; } if (horizontalVelocity < -horizontalMaxVelocity) { horizontalVelocity = -horizontalMaxVelocity; } } else { //Stop moving if velocity is within stop speed if (horizontalVelocity < horizontalStopSpeed && horizontalVelocity > -horizontalStopSpeed) { horizontalVelocity = 0; } //Slow down player in positive or negative direction else if (horizontalVelocity < 0) { horizontalVelocity += horizontalBreakSpeed * Time.fixedDeltaTime; } else { horizontalVelocity -= horizontalBreakSpeed * Time.fixedDeltaTime; } } //Check if the sides of the player are clear bool sideClear = true; if (horizontalVelocity < 0) { sideClear = CheckSide(true); } else if (horizontalVelocity > 0) { sideClear = CheckSide(false); } if (!sideClear) { horizontalVelocity = 0; } #endregion //Update real vertical and horizontal position (raycasts check if the ground has been clipped and make sure the player doesn't fall/walk through #region verticalClippingCheck float newVerticalPos = verticalPos + (verticalVelocity * Time.fixedDeltaTime); if (verticalVelocity < 0) { RaycastHit2D leftDownHit = Physics2D.Raycast(new Vector2(transform.position.x - 0.374f, verticalPos + -0.75f), Vector2.down, verticalPos - newVerticalPos, groundSurfaces); RaycastHit2D rightDownHit = Physics2D.Raycast(new Vector2(transform.position.x + 0.374f, verticalPos + -0.75f), Vector2.down, verticalPos - newVerticalPos, groundSurfaces); if (leftDownHit.collider != null) { newVerticalPos = verticalPos - leftDownHit.distance + 0.001f; } else if (rightDownHit.collider != null) { newVerticalPos = verticalPos - rightDownHit.distance + 0.001f; } } #endregion #region horizontalClippingCheck float newHorizontalPos = horizontalPos + (horizontalVelocity * Time.fixedDeltaTime); float xOffset = 0.375f; Vector2 direction = Vector2.right; float distance = newHorizontalPos - horizontalPos; if (horizontalVelocity < 0) { xOffset = -xOffset; direction = Vector2.left; distance = horizontalPos - newHorizontalPos; } RaycastHit2D UpHit = Physics2D.Raycast(new Vector2(transform.position.x + xOffset, verticalPos + 0.749f), direction, distance, groundSurfaces); RaycastHit2D MiddleHit = Physics2D.Raycast(new Vector2(transform.position.x + xOffset, verticalPos), direction, distance, groundSurfaces); RaycastHit2D DownHit = Physics2D.Raycast(new Vector2(transform.position.x + xOffset, verticalPos - 0.749f), direction, distance, groundSurfaces); if (UpHit.collider != null) { newHorizontalPos = horizontalPos - UpHit.distance; horizontalVelocity = 0; } else if (MiddleHit.collider != null) { newHorizontalPos = horizontalPos - MiddleHit.distance; horizontalVelocity = 0; } else if (DownHit.collider != null) { newHorizontalPos = horizontalPos - DownHit.distance; horizontalVelocity = 0; } #endregion //Update real positions horizontalPos = newHorizontalPos; verticalPos = newVerticalPos; if (!grounded) { if (currentSprite != "jumping") { spriteRenderer.sprite = jumpingSprite; currentSprite = "jumping"; } } else if (horizontalVelocity > horizontalStopSpeed || horizontalVelocity < -horizontalStopSpeed) { if (currentSprite != "walking") { spriteRenderer.sprite = walkingSprite; currentSprite = "walking"; } } else if (currentSprite != "standing") { spriteRenderer.sprite = standingSprite; currentSprite = "standing"; } //Round down position and apply to the transform transform.position = new Vector3(PixelRounder.RoundByPixels(horizontalPos, 32), PixelRounder.RoundByPixels(verticalPos, 32)); }