private MovementBounds CalculateMovementBounds() { var size = boxCollider.bounds.size; var horizontalSize = size - Vector3.up * (edgeCollidePreventionRatio * verticalExtents.y); var verticalSize = size - Vector3.right * (edgeCollidePreventionRatio * horizontalExtents.x); var hitLeftRay = Physics2D.BoxCastAll(position, horizontalSize, 0f, Vector2.left, size.x, layerMask.left) .FirstOrDefault(h => h.point.x < boxCollider.bounds.min.x); var hitRightRay = Physics2D.BoxCastAll(position, horizontalSize, 0f, Vector2.right, size.x, layerMask.right) .FirstOrDefault(h => h.point.x > boxCollider.bounds.max.x); var hitTopRay = Physics2D.BoxCastAll(position, verticalSize, 0f, Vector2.up, size.y, layerMask.top) .FirstOrDefault(h => h.point.y > boxCollider.bounds.max.y); var hitBottomRay = Physics2D.BoxCastAll(position, verticalSize, 0f, Vector2.down, size.y, layerMask.bottom) .FirstOrDefault(h => h.point.y < boxCollider.bounds.min.y); var bounds = new MovementBounds { left = hitLeftRay ? hitLeftRay.point.x + extents.x : Mathf.NegativeInfinity, right = hitRightRay ? hitRightRay.point.x - extents.x : Mathf.Infinity, top = hitTopRay ? hitTopRay.point.y - extents.y : Mathf.Infinity, bottom = hitBottomRay ? hitBottomRay.point.y + extents.y : Mathf.NegativeInfinity }; return(bounds); }
private bool IsWallJumping(MovementBounds bounds) { return(toggleableStates.wallJumping && state.Equals(STATE.WallSliding) && inputManager.inputJumpDown && ( (position.x.NearlyEqual(bounds.left) && inputManager.inputHorizontal >= 0) || (position.x.NearlyEqual(bounds.right) && inputManager.inputHorizontal <= 0) )); }
private bool IsWallDismounting(MovementBounds bounds) { return(state.Equals(STATE.WallSliding) && ( (position.x.NearlyEqual(bounds.left) && inputManager.inputHorizontal > 0) || (position.x.NearlyEqual(bounds.right) && inputManager.inputHorizontal < 0) )); }
private bool IsVerticalMovement(MovementBounds bounds) { return(!state.Equals(STATE.VerticalMovement) && !_velocity.y.NearlyEqual(0) && ( (!position.x.NearlyEqual(bounds.left) && inputManager.inputHorizontal < 0) || (!position.x.NearlyEqual(bounds.right) && inputManager.inputHorizontal > 0) )); }
private bool IsRunning(MovementBounds bounds) { return(!state.Equals(STATE.Running) && position.y.NearlyEqual(bounds.bottom) && (Mathf.Abs(inputManager.inputHorizontal) > 0 || Mathf.Abs(_velocity.x) > 0) && ( (!position.x.NearlyEqual(bounds.left) && inputManager.inputHorizontal <= 0) || (!position.x.NearlyEqual(bounds.right) && inputManager.inputHorizontal >= 0) )); }
private bool IsIdle(MovementBounds bounds) { return(!state.Equals(STATE.Idle) && ( position.y.NearlyEqual(bounds.bottom) && _velocity.x.NearlyEqual(0) || position.y.NearlyEqual(bounds.bottom) && position.x.NearlyEqual(bounds.left) || position.y.NearlyEqual(bounds.bottom) && position.x.NearlyEqual(bounds.right) )); }
private Vector2 MoveStep(MovementBounds bounds) { var nextPosition = position; nextPosition += _velocity * Time.fixedDeltaTime; nextPosition.x = Mathf.Clamp(nextPosition.x, bounds.left, bounds.right); nextPosition.y = Mathf.Clamp(nextPosition.y, bounds.bottom, bounds.top); return(nextPosition); }
private void LoopStateSwitch(MovementBounds bounds) { if (IsIdle(bounds)) { state = STATE.Idle; } else if (IsRunning(bounds)) { state = STATE.Running; } else if (IsWallDismounting(bounds)) { state = STATE.WallDismount; } else if (IsWallJumping(bounds)) { state = STATE.WallJump; } else if (IsWallSticking(bounds)) { state = STATE.WallSticking; } else if (IsWallSliding(bounds) || IsWallStickingExit(bounds)) { state = STATE.WallSliding; } else if (IsVerticalMovement(bounds)) { state = STATE.VerticalMovement; } else if (IsFalling(bounds)) { state = STATE.Fall; } else if (IsJumping()) { state = STATE.Jump; } }
private bool IsWallSliding(MovementBounds bounds) { return(!state.Equals(STATE.WallSliding) && !state.Equals(STATE.WallSticking) && (position.x.NearlyEqual(bounds.left) || position.x.NearlyEqual(bounds.right)) && !position.y.NearlyEqual(bounds.top) && !position.y.NearlyEqual(bounds.bottom)); }
private bool IsFalling(MovementBounds bounds) { return(!state.Equals(STATE.VerticalMovement) && !state.Equals(STATE.WallSliding) && !state.Equals(STATE.WallSticking) && !position.y.NearlyEqual(bounds.bottom) && _velocity.y <= 0 || position.y.NearlyEqual(bounds.top)); }
private void Start() { m_movementBounds = FindObjectOfType <MovementBounds>(); SetRandomDestination(); }