private void CheckGrounded() { Grounded = false; Ground = null; float gravitySign = Mathf.Sign(gravity); Vector2 rayBaseOrigin = (Vector2)transform.position + Vector2.left * Collider.bounds.size.x / 2 + gravitySign * Vector2.up * Collider.bounds.size.y / 2; float raySpacing = Collider.bounds.size.x / (numGroundRays - 1); for (int i = 0; i < numGroundRays; i++) { Vector2 origin = rayBaseOrigin + Vector2.right * raySpacing * i; RaycastHit2D hit = Physics2D.Raycast(origin, Vector2.up * gravitySign, groundRayLength, groundMask); if (hit.collider && !hit.collider.isTrigger) { Grounded = true; Ground = hit.collider; Debug.DrawRay(origin, Vector2.up * gravitySign * groundRayLength, Color.green); onGrounded?.Invoke(); break; } Debug.DrawRay(origin, Vector2.up * gravitySign * groundRayLength, Color.red); } }
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; }