public void Move(float move, bool jump, bool shove) { if (mStunLeft > 0f) { return; } //only control the player if grounded or airControl is turned on if (m_Grounded || m_AirControl) { // Move the character by finding the target velocity Vector3 targetVelocity = new Vector2(move * m_Speed, m_Rigidbody2D.velocity.y); // And then smoothing it out and applying it to the character m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing); // If the input is moving the player right and the player is facing left... if (move > 0 && !m_FacingRight) { // ... flip the player. Flip(); } // Otherwise if the input is moving the player left and the player is facing right... else if (move < 0 && m_FacingRight) { // ... flip the player. Flip(); } animController.SetRunning(Mathf.Abs(move) > 0.01f); } // If the player should jump... if (m_Grounded && jump) { // Add a vertical force to the player. m_Grounded = false; m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce)); animController.Jump(); JumpSound.Play(); } if (shove) { animController.Shove(); // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground // This can be done using layers instead but Sample Assets will not overwrite your project settings. Collider2D[] colliders = Physics2D.OverlapCircleAll(m_ShoveCheck.position, k_ShoveRadius); for (int i = 0; i < colliders.Length; i++) { if (colliders[i].gameObject != gameObject && colliders[i].gameObject.GetComponent <CharacterController2D>()) { colliders[i].gameObject.GetComponent <Rigidbody2D>().AddForce((colliders[i].gameObject.transform.position - transform.position).normalized * ShoveForce, ForceMode2D.Impulse); colliders[i].gameObject.GetComponentInChildren <CharacterAnimationController>().GetShoved(); colliders[i].gameObject.GetComponentInChildren <CharacterController2D>().ShoveStun(); } } } }
void FixedUpdate() { isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); if (isGrounded && velocity.y < 0) { velocity.y = -2f; } if (jump && isGrounded) { animationController.Jump(); jump = false; jumping = true; } if (jumping && jumpFrame <= jumpFrames) { jumpFrame += 1; Vector3 moveDirection = new Vector3(0, 0, 0); moveDirection.y = Mathf.Lerp(0, jumpHeight, jumpLerpStep); transform.Translate(moveDirection); if (jumpFrame == jumpFrames) { jumpFrame = 0; jumping = false; } } if (!jumping) { velocity.y += gravity * Time.fixedDeltaTime; controller.Move(velocity * Time.fixedDeltaTime); } }
internal void Jump() { characterMovement.Jump(); characterAnimation.Jump(); }