Search Unity

Booleans Interfering With One Another

Discussion in 'Scripting' started by TwisstedSmiley, May 7, 2020.

  1. TwisstedSmiley

    TwisstedSmiley

    Joined:
    May 7, 2020
    Posts:
    4
    I have 2 Booleans which define;
    - if the player is seen (A)
    - if another NavmeshAgent is within a radius (B)
    i would like that if the player is not seen but if a navmesh agent is within the radius, the agents go to one another. For some reason if i put both in the Update(), B will not read as true unless A is true and vice versa. Ideas?

    public class PlayerController : MonoBehaviour
    {

    public NavMeshAgent agent;
    public Transform player;
    public float maxAngle;
    public float maxRadius;
    public float maxHear;
    private bool isInFOV = false;
    private bool isInAudio = false;
    public static bool inFOV(Transform checkingObject, Transform target, float maxAngle, float maxRadius)
    {
    Collider[] overlaps = new Collider[10];
    int count = Physics.OverlapSphereNonAlloc(checkingObject.position, maxRadius, overlaps);
    for (int i = 0; i < count + 1; i++)
    {
    if (overlaps[1] != null)
    {
    if (overlaps[i].transform == target)
    {
    UnityEngine.Vector3 directionBetween = (target.position - checkingObject.position).normalized;
    directionBetween.y *= 0;

    float angle = UnityEngine.Vector3.Angle(checkingObject.forward, directionBetween);

    if (angle <= maxAngle)
    {
    Ray Sight = new Ray(checkingObject.position, target.position - checkingObject.position);
    RaycastHit hit;

    if (Physics.Raycast(Sight, out hit, maxRadius))
    {

    if (hit.transform == target)
    {
    return true;
    }
    }
    }
    }
    }
    }

    return false;
    }
    public static bool inAudio(Transform checkingObject1, Transform target1, float maxHear)
    {
    Collider[] overlaps1 = new Collider[10];
    int count1 = Physics.OverlapSphereNonAlloc(checkingObject1.position, maxHear, overlaps1);
    for (int i = 0; i < count1 + 1; i++)
    {
    if (overlaps1[1] != null)
    {
    if (overlaps1[i].transform == target1)
    {
    return true;
    }
    }
    }
    return false;
    }
    private void Start()
    {
    agent = GetComponent<NavMeshAgent>();

    }

    // Update is called once per frame

    void Update()
    {

    GameObject[] gos;
    gos = GameObject.FindGameObjectsWithTag("Zombie");
    GameObject closest = null;
    float distance = Mathf.Infinity;

    foreach (GameObject go in gos)
    {
    UnityEngine.Vector3 diff = go.transform.position - agent.transform.position;
    float curDistance = diff.sqrMagnitude;
    if (curDistance < distance && curDistance > 0)
    {
    closest = go;
    distance = curDistance;

    }

    }


    isInFOV = inFOV(transform, player, maxAngle, maxRadius);
    isInAudio = inAudio(transform, closest.transform, maxHear);
    print(isInAudio);

    if (isInFOV == true)
    {
    isInAudio = false;
    agent.SetDestination(player.position);
    isInFOV = false;
    }


    if (isInAudio == true)
    {
    agent.SetDestination(closest.transform.position);
    isInAudio = false;
    }





    }
    }

     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Code (CSharp):
    1.         if (isInFOV == true)
    2.         {
    3.             isInAudio = false;
    4.             agent.SetDestination(player.position);
    5.             isInFOV = false;
    6.         }
    You're explicitly setting isInAudio to false if isInFOV is true...
     
  3. TwisstedSmiley

    TwisstedSmiley

    Joined:
    May 7, 2020
    Posts:
    4

    if (isInFOV == true)
    {

    agent.SetDestination(player.position);

    }


    if (isInAudio == true)
    {
    agent.SetDestination(closest.transform.position);

    }


    this results in the agents moving together if they can see the player, but no action otherwise
     
  4. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    Code (CSharp):
    1. if (isInFOV == true)
    2.         {
    3.        
    4.             agent.SetDestination(player.position);
    5.        
    6.         }
    7.      
    8.    
    9.         if (isInAudio == true && isInFOV == false)
    10.         {
    11.             agent.SetDestination(closest.transform.position);
    12.        
    13.         }
     
  5. TwisstedSmiley

    TwisstedSmiley

    Joined:
    May 7, 2020
    Posts:
    4
    Was just trying this, sadly does not work. makes agents follow player but only when they are in "audio" range. Seeminly for isInFOV to be true, isInAudio must also be true, thats the only way i can see the issue arising
     
  6. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    do you have any scripts on your zombie objects?
     
  7. TwisstedSmiley

    TwisstedSmiley

    Joined:
    May 7, 2020
    Posts:
    4
    This script is on the zombie object