/// <summary> /// Updates the bounds of the capsule collider. The Capsule collider is made up of one rect bounds and two circle bounds /// </summary> public override void UpdateBoundsOfCollider() { BoundsRect b = new BoundsRect(); Vector2 origin = this.transform.position + new Vector3(colliderOffset.x, colliderOffset.y); float xSize = drawHorizontal ? size : radius * 2; float ySize = drawHorizontal ? radius * 2 : size; b.topLeft = origin + Vector2.up * ySize / 2 - Vector2.right * xSize / 2; b.topRight = origin + Vector2.up * ySize / 2 + Vector2.right * xSize / 2; b.bottomLeft = origin - Vector2.up * ySize / 2 - Vector2.right * xSize / 2; b.bottomRight = origin - Vector2.up * ySize / 2 + Vector2.right * xSize / 2; bounds.rectBounds = b; BoundsCircle topCircle = new BoundsCircle(); topCircle.center = b.topLeft + (b.topRight - b.topLeft) / 2f; topCircle.radius = radius; bounds.topCircleBounds = topCircle; BoundsCircle bottomCircle = new BoundsCircle(); bottomCircle.center = b.bottomLeft + (b.bottomRight - b.bottomLeft) / 2f; bottomCircle.radius = radius; bounds.bottomCircleBounds = bottomCircle; if (!isStatic) { verticalBounds = bounds; verticalBounds.topCircleBounds.radius = bounds.topCircleBounds.radius - colliderBuffer; verticalBounds.bottomCircleBounds.radius = bounds.bottomCircleBounds.radius - colliderBuffer; verticalBounds.rectBounds.topLeft.x = bounds.rectBounds.topLeft.x + colliderBuffer / 2; verticalBounds.rectBounds.topRight.x = bounds.rectBounds.topRight.x - colliderBuffer / 2; verticalBounds.rectBounds.bottomLeft.x = verticalBounds.rectBounds.topLeft.x; verticalBounds.rectBounds.bottomRight.x = verticalBounds.rectBounds.topRight.x; horizontalBounds = bounds; horizontalBounds.topCircleBounds.radius -= colliderBuffer; horizontalBounds.bottomCircleBounds.radius -= colliderBuffer; //horizontalBounds.topCircleBounds.center += Vector2.down * colliderBuffer; //horizontalBounds.bottomCircleBounds.center += Vector2.up * colliderBuffer; Vector2 horizontalOffset = Vector2.zero; Vector2 verticalOffset = Vector2.zero; if (rigid.Velocity.y > 0) { verticalOffset = Vector2.up * Mathf.Max(colliderBuffer, rigid.Velocity.y * Overseer.DELTA_TIME); } else if (rigid.Velocity.y < 0) { verticalOffset = Vector2.up * Mathf.Min(-colliderBuffer, rigid.Velocity.y * Overseer.DELTA_TIME); } if (rigid.Velocity.x > 0) { horizontalOffset = Vector2.right * Mathf.Max(colliderBuffer, rigid.Velocity.x * Overseer.DELTA_TIME); } else if (rigid.Velocity.x < 0) { horizontalOffset = Vector2.right * Mathf.Min(-colliderBuffer, rigid.Velocity.x * Overseer.DELTA_TIME); } verticalBounds.SetOffset(verticalOffset); horizontalBounds.SetOffset(horizontalOffset); } }