/// <summary>
        /// Handles the event when the character triggers the ground
        /// </summary>
        public void Handle(OnGrounded @event)
        {
            _locomotionHandler.ChangeDetectionCollisionMode(CollisionDetectionMode2D.Discrete);
            _locomotionHandler.SetGroundGravityScale();
            _locomotionHandler.Stop();

            if (isJustEntered)
            {
                isJustEntered = false;
                ToggleStateMachineContexts(true);
                ToggleControls(true);
            }
        }
예제 #2
0
    bool IsGrounded()
    {
        Vector3   pointBottom = transform.position - transform.up * groundMargin + transform.up * groundDetectRadius;
        Vector3   pointTop    = transform.position + transform.up * groundDetectRadius;
        LayerMask ignoreMask  = ~(1 << 9);

        //Debug.DrawLine(pointBottom, pointTop, Color.green);
        //Debug.Log(Physics.OverlapCapsule(pointBottom, pointTop, groundDetectRadius, ignoreMask, QueryTriggerInteraction.Ignore)[0]);
        if (Physics.OverlapCapsule(pointBottom, pointTop, groundDetectRadius, ignoreMask, QueryTriggerInteraction.Ignore).Length != 0)
        {
            //Debug.Log(Physics.OverlapCapsule(pointBottom, pointTop, groundDetectRadius, ignoreMask, QueryTriggerInteraction.Ignore)[0]);
            isGrounded = true;
        }
        else
        {
            isGrounded = false;
        }

        if (!wasGrounded && isGrounded)
        {
            wasGrounded = true;
            OnGrounded?.Invoke(true);
        }
        else if (wasGrounded && !isGrounded)
        {
            wasGrounded = false;
            OnGrounded?.Invoke(false);
        }

        return(isGrounded);

        //return isGrounded;
        //isGrounded |= Physics.Raycast(transform.position, -Vector3.up, groundMargin, 1, QueryTriggerInteraction.Ignore);
        //isGrounded |= Physics.Raycast(transform.position + transform.forward, -Vector3.up, groundMargin, 1, QueryTriggerInteraction.Ignore);
        //return Physics.Raycast(transform.position, -Vector3.up, groundMargin, 1, QueryTriggerInteraction.Ignore);   // Watch out the layer!!!
    }
        /// <summary>
        /// Checks ground bellow character
        /// </summary>
        private void CheckGround()
        {
            if (GroundCheckDistance > 0.05f)
            {
                Vector3 Origin = transform.position + Vector3.up;

                if (Physics.SphereCast(Origin, m_Capsule.radius, Vector3.down, out m_GroundHit, 2f + m_GroundCheckDistance, m_GroundMask, QueryTriggerInteraction.Ignore))
                {
                    float distance = transform.position.y - m_GroundHit.point.y;

                    if (distance > -m_GroundCheckDistance && distance <= m_GroundCheckDistance)
                    {
                        if (m_GroundHit.normal.y > Mathf.Cos(m_MaxAngleSlope * Mathf.Deg2Rad)) // Calculate the angle of the ground. If it's higher than maxSlope, don't be grounded
                        {
                            if (!IsGrounded)
                            {
                                if (m_Rigidbody.velocity.y < -6)
                                {
                                    OnGrounded.Invoke();
                                }

                                m_LastGroundPos = m_GroundHit.transform.position;
                                m_LastAngle     = m_GroundHit.transform.rotation.eulerAngles.y;
                            }

                            IsGrounded    = true;
                            GroundNormal  = m_GroundHit.normal;
                            GroundHitInfo = m_GroundHit;
                            return;
                        }
                        else
                        {
                            Origin += m_Rigidbody.velocity.normalized * (m_Capsule.radius + 0.05f);
                            if (Physics.Raycast(Origin, Vector3.down, out m_GroundHit, 2f + m_GroundCheckDistance, m_GroundMask, QueryTriggerInteraction.Ignore))
                            {
                                distance = transform.position.y - m_GroundHit.point.y;

                                if (distance > -m_GroundCheckDistance && distance <= m_GroundCheckDistance)
                                {
                                    if (m_GroundHit.normal.y > Mathf.Cos(m_MaxAngleSlope * Mathf.Deg2Rad)) // Calculate the angle of the ground. If it's higher than maxSlope, don't be grounded
                                    {
                                        if (!IsGrounded)
                                        {
                                            if (m_Rigidbody.velocity.y < -6)
                                            {
                                                OnGrounded.Invoke();
                                            }

                                            m_LastGroundPos = m_GroundHit.transform.position;
                                            m_LastAngle     = m_GroundHit.transform.rotation.eulerAngles.y;
                                        }

                                        IsGrounded    = true;
                                        GroundNormal  = m_GroundHit.normal;
                                        GroundHitInfo = m_GroundHit;
                                        return;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            IsGrounded   = false;
            GroundNormal = Vector3.up;
        }