Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Combining Multiple Behaviours (Wander and Follow)

Discussion in 'Scripting' started by greburt, Mar 4, 2020.

  1. greburt

    greburt

    Joined:
    Feb 27, 2020
    Posts:
    9
    Hi all,

    I'm a newbie, hopefully I'm posting this in the right section.

    I'm trying to learn to code simple character behaviors. I've created a small world where a zombie spawns and is supposed to walk to the player, if within a certain distance. Otherwise, it should just wander around.

    I found a wandering script on the forums that works well. I also setup a sphere collider (with trigger) on the zombie, with script that causes it to follow the player if triggered. These behaviors work if run separately.

    The problem is I can't seem to make the two behaviors work together. Ideally, I want something to the effect of "If within sphere then follower player, else wander". I can't figure out how to structure an if statement to achieve this. Any help would be appreciated. Thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class ZombieBehaviour : MonoBehaviour
    7. {
    8.     public GameObject target;
    9.     private UnityEngine.AI.NavMeshAgent zombieNav;
    10.     public float wanderRadius;
    11.     public float wanderTimer;
    12.     private float timer;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         zombieNav = GetComponent<UnityEngine.AI.NavMeshAgent>();
    18.         timer = wanderTimer;
    19.     }
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         timer += Time.deltaTime;
    24.         if (timer >= wanderTimer)
    25.         {
    26.             Vector3 newPos = RandomNavSphere(transform.position, wanderRadius, -1);
    27.             zombieNav.SetDestination(newPos);
    28.             timer = 0;
    29.         }
    30.     }
    31.     public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask)
    32.     {
    33.         Vector3 randDirection = Random.insideUnitSphere * dist;
    34.         randDirection += origin;
    35.         UnityEngine.AI.NavMeshHit navHit;
    36.         UnityEngine.AI.NavMesh.SamplePosition(randDirection, out navHit, dist, layermask);
    37.         return navHit.position;
    38.     }
    39.     private void OnTriggerEnter(Collider other)
    40.     {
    41.         if (other.gameObject.tag == "Target")
    42.         {
    43.             zombieNav.SetDestination(target.transform.position);
    44.         }
    45.     }
    46.  
    47. }
     
  2. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    You could use a boolean like "followPlayer" and set it to true inside triggerenter then in the update function if followPlayer is true move to player else wander
     
  3. greburt

    greburt

    Joined:
    Feb 27, 2020
    Posts:
    9
    Thanks for your answer :)