/// <summary> /// Called when grounded value has been set. /// </summary> protected override void OnSetGrounded() { base.OnSetGrounded(); if (isGrounded) { hasJustLanded = true; StopJump(); if (wallStuckState != WallStuck.None) { WallStuckState = WallStuck.None; } transform.rotation = Quaternion.identity; animator.SetInteger(anim_GroundStateID, 0); if (!ConsumeJumpBuffer() && (Time.time - SlideBufferVar < attributes.SlideBufferTime) && (Input.GetKey(KeyCode.C) || Input.GetKey(KeyCode.Joystick1Button1)) && (Mathf.Abs(lastMovement + force.x) > attributes.SlideRequiredVelocity)) { Slide(); } } // Set last time the player left a platform. else { GetUp(); coyoteTimeVar = Time.time; } }
private void CheckWallStatus() { WallStuck _state = WallStuck.None; // Get first cast direction. float _direction = (wallStuckState == 0) ? Mathf.Sign(movement.x + force.x) : (int)wallStuckState; Vector2 _movement = new Vector2(_direction * Physics2D.defaultContactOffset * 2.5f, 0); // Perform cast in given direction, and opposite one if nothing is hit. if (!CastForWall()) { _direction *= -1; _movement.x *= -1; CastForWall(); } if (wallStuckState != _state) { // Face wall opposite side. if (_state != 0) { Flip(-(int)_state); transform.rotation = Quaternion.identity; ResetMovement(); ConsumeJumpBuffer(); } WallStuckState = _state; } // Set wall state when hitting a vertical collider. bool CastForWall() { if (CastCollider(_movement, out RaycastHit2D _hit)) { if (_hit.normal.x == -_direction) { _state = (WallStuck)_direction; } return(true); } return(false); } }
private bool Jump() { // Perform wall jump if against one. if (wallStuckState != WallStuck.None) { StopSlide(); if (isJumping) { StopCoroutine(jumpCoroutine); } jumpCoroutine = StartCoroutine(WallJump()); return(true); } // Perform a normal jump if on ground or was recently. if (isGrounded || (((Time.time - coyoteTimeVar) < attributes.CoyoteTime)) && !isJumping) { StopSlide(); jumpCoroutine = StartCoroutine(HighJump()); return(true); } // Try to perform wall jump, if near to a wall. float _direction = facingSide * ((Physics2D.defaultContactOffset * 2) + attributes.WallJumpGap); if (CastCollider(new Vector2(_direction, 0), out RaycastHit2D _hit) || CastCollider(new Vector2(_direction *= -1, 0), out _hit)) { // Set corresponding wall stuck state. WallStuckState = (WallStuck)Mathf.Sign(_direction); StopSlide(); if (isJumping) { StopCoroutine(jumpCoroutine); } jumpCoroutine = StartCoroutine(WallJump()); return(true); } return(false); }