Beispiel #1
0
        private void UpdateMovement()
        {
            GroundCheck();

            // If move by WASD keys, set move direction to input direction
            if (tempInputDirection.magnitude > 0f)
            {
                tempMoveDirection = tempInputDirection;
            }

            if (IsDead())
            {
                tempMoveDirection = Vector3.zero;
                IsJumping         = false;
            }

            if (tempMoveDirection.magnitude > 0f)
            {
                tempMoveDirection = tempMoveDirection.normalized;

                // always move along the camera forward as it is the direction that it being aimed at
                tempMoveDirection = Vector3.ProjectOnPlane(tempMoveDirection, groundContactNormal).normalized;

                float currentTargetSpeed = gameInstance.GameplayRule.GetMoveSpeed(this);
                // If character move backward
                if (Vector3.Angle(tempMoveDirection, CacheTransform.forward) > 120)
                {
                    currentTargetSpeed *= backwardMoveSpeedRate;
                }

                tempMoveDirection *= currentTargetSpeed;
                if (IsGrounded)
                {
                    CacheRigidbody.velocity = tempMoveDirection;
                }
                else
                {
                    CacheRigidbody.velocity = new Vector3(tempMoveDirection.x, CacheRigidbody.velocity.y, tempMoveDirection.z);
                }
            }
            else
            {
                CacheRigidbody.velocity = new Vector3(0f, CacheRigidbody.velocity.y, 0f);
            }

            if (IsGrounded)
            {
                CacheRigidbody.drag = 5f;

                if (IsJumping)
                {
                    RequestTriggerJump();
                    CacheRigidbody.drag     = 0f;
                    CacheRigidbody.velocity = new Vector3(CacheRigidbody.velocity.x, 0f, CacheRigidbody.velocity.z);
                    CacheRigidbody.AddForce(new Vector3(0f, CalculateJumpVerticalSpeed(), 0f), ForceMode.Impulse);
                    applyingJump = true;
                    IsGrounded   = false;
                }

                if (!applyingJump &&
                    Mathf.Abs(tempMoveDirection.x) < float.Epsilon &&
                    Mathf.Abs(tempMoveDirection.z) < float.Epsilon &&
                    CacheRigidbody.velocity.magnitude < 1f)
                {
                    CacheRigidbody.Sleep();
                }
            }
            else
            {
                CacheRigidbody.drag = 0f;
                if (previouslyGrounded && !applyingJump)
                {
                    StickToGroundHelper();
                }
            }
            IsJumping = false;
        }