void VerticalCollisions(ref Vector2 moveAmount) { float directionY = Mathf.Sign(moveAmount.y); float rayLength = Mathf.Abs(moveAmount.y) + skinWidth; for (int i = 0; i < verticalRayCount; i++) { Vector2 rayOrigin = raycastOrigins.bottomLeft; rayOrigin += Vector2.right * (verticalRaySpacing * i + moveAmount.x); RaycastHit2D[] hits = Physics2D.RaycastAll(rayOrigin, Vector2.up * directionY, rayLength, collisionMask); Debug.DrawRay(rayOrigin, Vector2.up * directionY, Color.red); for (int j = 0; j < hits.Length; j++) { if (hits[j]) { if (hits[j].transform.gameObject == transform.gameObject) { continue; } if (hits[j].transform.CompareTag("Lava")) { if (cube.velocity.y < 0) { cube.Break(); } continue; } if (hits[j].transform.CompareTag("Solid")) { if (cube.isDangerous) { cube.Break(); } if (cube.velocity.y < 0) { cube.Break(); } } if (hits[j].transform.CompareTag("Hat")) { Hat otherHat = hits[j].transform.GetComponent <Hat>(); if (!otherHat.isBeingThrown) { otherHat.BlowBack(otherHat.transform.position - transform.position, cube.velocity.magnitude); } } if (hits[j].transform.CompareTag("Player")) { Player otherPlayer = hits[j].transform.GetComponent <Player>(); if (cube.isDangerous) { Vector3 hitDirection = otherPlayer.transform.position - transform.position; otherPlayer.BlowBack(hitDirection, 40, 0.2f); cube.Break(); } else if (cube.velocity.y >= 0) { continue; } if (cube.velocity.y < 0) { cube.Break(); otherPlayer.GetFootStooled(0.2f, false); } } moveAmount.y = (hits[j].distance - skinWidth) * directionY; rayLength = hits[j].distance; if (!hits[j].transform.CompareTag("Player") && !hits[j].transform.CompareTag("Hat") && hits[j].transform.gameObject != transform.gameObject) { collisions.below = directionY == -1; collisions.above = directionY == 1; } } } } }
void HorizontalCollisions(ref Vector2 moveAmount) { //if (hat.collisionSkip) return; Vector2 oldVelocity = cube.velocity; float directionX = (cube.velocity.x == 0) ? 0 : collisions.faceDir; float rayLength = Mathf.Abs(moveAmount.x) + skinWidth; if (Mathf.Abs(moveAmount.x) < skinWidth) { rayLength = 2 * skinWidth; } for (int i = 0; i < horizontalRayCount; i++) { Vector2 rayOrigin = (directionX == -1) ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight; rayOrigin += Vector2.up * (horizontalRaySpacing * i); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collisionMask); Debug.DrawRay(rayOrigin, Vector2.right * directionX, Color.red); if (hit) { if (hit.transform.gameObject == transform.gameObject) { continue; } if (hit.distance == 0) { //continue; } if (hit.collider.CompareTag("Ignore")) { continue; } if (hit.collider.CompareTag("Solid")) { if (!cube.isDangerous) { cube.Break(); } else { collisions.shouldReflect = true; } continue; } if (hit.collider.CompareTag("Lava")) { if (cube.velocity.y < 0) { cube.Break(); } continue; } if (hit.collider.CompareTag("Player")) { Player otherPlayer = hit.collider.GetComponent <Player>(); if (cube.isDangerous) { Vector3 hitDirection = ((Vector2)otherPlayer.transform.position - hit.point).normalized; otherPlayer.BlowBack(hitDirection, 40, 0.2f); } if (cube.velocity.x > 1 || cube.velocity.x < -1) { cube.Break(); } } if (hit.collider.CompareTag("Hat")) { Hat otherHat = hit.transform.GetComponent <Hat>(); if (!otherHat.isBeingThrown) { otherHat.BlowBack(otherHat.transform.position - transform.position, cube.velocity.magnitude); cube.Break(); } } moveAmount.x = (hit.distance - skinWidth) * directionX; rayLength = hit.distance; collisions.left = directionX == -1; collisions.right = directionX == 1; if ((collisions.right || collisions.left)) { collisions.shouldReflect = true; } } } }
public virtual void FixedUpdate() { if (GameController.instance.game.state == Game.State.PAUSED) { return; } //dev tool if (Input.GetKeyDown(KeyCode.P)) { InitGravityValues(); } GetCurrentInput(); if (deathFreeze) { if (Time.time > deathFreezeTime) { deathFreeze = false; wasKilled = true; } else { return; } } if (hitFreeze) { if (freezeFrames < hitFreezeLength) { freezeFrames++; } else { freezeFrames = 0; hitFreeze = false; } return; } if (invulnerable) { if (Time.time > invulnerabilityTimer) { EndInvulnerability(); } } CalculateVelocity(); HandleWallSliding(); controller.Move(velocity * Time.fixedDeltaTime, directionalInput, false, isDead); if ((controller.collisions.above || controller.collisions.below)) { // if (controller.collisions.slidingDownMaxSlope) { // velocity.y += controller.collisions.slopeNormal.y * -gravity * Time.fixedDeltaTime; // } else { // velocity.y = 0; // } velocity.y = 0; } if (controller.collisions.below) { jumpsCompleted = 0; hoversCompleted = 0; superJumping = false; if (!teleporting) { teleportsCompleted = 0; } } if (controller.collisions.shouldBounce) { velocity.y = bounceVelocity; controller.collisions.shouldBounce = false; StartBounce(); canHurtHat = true; } else if (controller.collisions.shouldBounceOnBouncyPlatform) { velocity.y = bouncePlatformVelocity; controller.collisions.shouldBounceOnBouncyPlatform = false; StartBounce(); } else if (controller.collisions.shouldBounceOutOfWell) { velocity.y = bounceOutOfWellVelocity; controller.collisions.shouldBounceOutOfWell = false; StartBounce(); } else if (controller.collisions.shouldBounceOnLava) { velocity.y = bounceOffLavaVelocity; velocity.x = (velocity.x <= 1 && velocity.x >= -1) ? ((velocity.x >= 0) ? 10 : -10) : velocity.x; controller.collisions.shouldBounceOnLava = false; StartBounce(); if (hat.isCurrentlyAttached) { hat.BlowBack(new Vector2(-velocity.x, velocity.y / 2), 0.3f); //what % of player blow back is applied to the hat } } }