Example #1
0
    private void OnTriggerEnter(Collider other)
    {
        // We detect a pickup in our radius
        if (other.gameObject.tag == TagStatics.GetPickupTag())
        {
            if (InLineOfSight(other.gameObject))
            {
                OnTargetClearLOS?.Invoke(other.gameObject);
                OnTargetInRadius?.Invoke(other.gameObject);
                _isTrackingTarget = true;
            }
            else
            {
                OnTargetInRadius?.Invoke(other.gameObject);
                _isTrackingTarget = false;
            }
        }

        if (other.gameObject.tag == TagStatics.GetMobTag() && other.gameObject.name == TagStatics.GetPlayerName())
        {
            //The mob is within our LoS
            if (InLineOfSight(other.gameObject))
            {
                OnTargetClearLOS?.Invoke(other.gameObject);
                OnTargetInRadius?.Invoke(other.gameObject);
                _isTrackingTarget = true;
            }
            else
            {
                OnTargetInRadius?.Invoke(other.gameObject);
                _isTrackingTarget = false;
                return;
            }
            currentTarget = other.gameObject;
        }


        //We heard a noise
        if (other.gameObject.tag == "Audible")
        {
            OnHeardSound?.Invoke(other.gameObject);
        }
    }
Example #2
0
    /// <summary>
    /// This is to check if we have an unobstructed LoS to whoever enters the monster's search radius
    /// If it's unobstructed, we know that we can "see" them
    /// </summary>
    private bool InLineOfSight(GameObject obj)
    {
        var        startPos = gameObject.transform.position;
        var        endPos   = (obj.transform.position - gameObject.transform.position);
        RaycastHit hit;

        if (Physics.Raycast(startPos, endPos, out hit) == true)
        {
            if (hit.collider.gameObject.name == obj.name)
            {
                OnTargetClearLOS?.Invoke(obj);
                OnTargetInRadius?.Invoke(obj);
                Debug.DrawRay(startPos, hit.point - startPos, Color.green);
                return(true);
            }
            else
            {
                OnTargetInRadius?.Invoke(obj);
                return(false);
            }
        }

        return(false);
    }