Enemy raycast script

 avatar
unknown
csharp
2 years ago
1.1 kB
29
Indexable
    /// <summary>
    /// Should the enemy turn when patrolling
    /// </summary>
    public bool PatrolTurn()
    {
        float feetColliderHeight = Mathf.Abs(m_feetCollider.bounds.max.y - m_feetCollider.bounds.min.y);

        RaycastHit2D raycastHit = Physics2D.Raycast(new Vector2(m_feetCollider.bounds.center.x, m_feetCollider.bounds.max.y), 
            Vector2.down, 
            feetColliderHeight, 
            LayerMask.GetMask("Ground"));

        #region DrawLine
#if UNITY_EDITOR
        Color rayColor = Color.black;

        bool touchingGround = raycastHit.collider != null;

        if (touchingGround)
        {
            rayColor = Color.green;
        }
        else
        {
            rayColor = Color.red;
        }

        Debug.DrawRay(new Vector2(m_feetCollider.bounds.center.x, m_feetCollider.bounds.max.y), Vector2.down * (feetColliderHeight), rayColor);
#endif
        #endregion

        return !touchingGround ||
                m_headCollider.IsTouchingLayers(LayerMask.GetMask("Ground"));
    }
Editor is loading...