private void GetGroundY() { RaycastHit2D rayHit = movement.GetGround(); float groundY; if (rayHit == new RaycastHit2D()) { gapDetected = true; if (gapHover != GapHoverState.ENDED) { gapHover = GapHoverState.HOVER; } lastRelativeGroundY = lastRayHit.distance; return; } gapDetected = false; gapHover = GapHoverState.NONE; time = 0; relativeGroundY = rayHit.distance; lastRelativeGroundY = relativeGroundY; if (rayHit.transform.gameObject.layer != LayerMask.NameToLayer("Dynamic")) { lastRayHit = rayHit; } }
void FixedUpdate() { GetGroundY(); AddHoverForce(); if (!gapDetected && gapHover == GapHoverState.ENDED) { gapHover = GapHoverState.NONE; } RandomForce(); if (gapHover == GapHoverState.HOVER || movement.GetJump() == JumpState.INITIATED) { hoverEffect.Play(); } else { hoverEffect.Stop(); } }
private void AddHoverForce() { float yPosition = relativeGroundY; float hoverForce = maxForce; float highDistance = maxDistance; float lowDistance = minDistance; if (gapHover == GapHoverState.HOVER) { if (gapHoverTime <= time) { time = 0; gapHover = GapHoverState.ENDED; return; } yPosition = lastRelativeGroundY; time++; highDistance *= gapDistanceMultiplier; hoverForce *= gapForceMultiplier; lowDistance *= gapMinDistanceMultiplier; } if (gapHover == GapHoverState.ENDED) { return; } if (movement.GetInput().y != 0 && movement.GetJump() == JumpState.INITIATED) { return; } float force = (highDistance - yPosition) / (highDistance - lowDistance); if (force < 0) { return; } force *= hoverForce; rb.AddForce(Vector2.up * force); }