Search Unity

Chase Script not working properly

Discussion in 'Scripting' started by denissuu, Feb 16, 2020.

  1. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162
    Hi there everyone! I currently have a Patrol Script that also has a chase script, and the problem is that when he is supposed to patrol, he randomly gets in that distance of chase, but he is above me then gets stuck there if I don't move, because he can't decide if he wants to chase or patrol while trying to come downstairs for me.

    How could I modify my script so that when he is not at the same floor ( Y position ) as me, the chase script can't be triggered?

    Here is the code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. public class Patrol : MonoBehaviour
    6. {
    7.     public Transform player;
    8.     public Transform[] moveSpots;
    9.     private int randomSpot;
    10.     NavMeshAgent nav;
    11.     public float distToPlayer = 5.0f;
    12.     public float chaseRadius = 20f;
    13.     public Rigidbody sandu;
    14.     public GameObject PlayerControls;
    15.     public GameObject playercam;
    16.     public GameObject killcam;
    17.     public GameObject MainMenu;
    18.  
    19.     private float waitTime;
    20.     public float killwaitTime = 1f;
    21.     public float startWaitTime = 1f;
    22.     public float menuWaitTime = 1f;
    23.     Animator anim;
    24.     public float distance;
    25.  
    26.     void Start()
    27.     {
    28.         anim = GetComponent<Animator>();
    29.         waitTime = startWaitTime;
    30.         randomSpot = Random.Range(0, moveSpots.Length);
    31.     }
    32.  
    33.     private void Awake()
    34.     {
    35.         nav = GetComponent<NavMeshAgent>();
    36.         nav.enabled = true;
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.         distance = Vector3.Distance(player.position, this.transform.position);
    43.  
    44.         if (distance > chaseRadius)
    45.         {
    46.             Patroling();
    47.          
    48.         }
    49.         else if (distance <= chaseRadius)
    50.         {
    51.             ChasePlayer();
    52.         }
    53.     }
    54.  
    55.     void Patroling()
    56.     {
    57.         nav.SetDestination(moveSpots[randomSpot].position);
    58.         if (Vector3.Distance(transform.position, moveSpots[randomSpot].position) < 5.0f)
    59.         {
    60.             if (waitTime <= 0)
    61.             {
    62.                 anim.SetInteger("condition", 2);
    63.                 randomSpot = Random.Range(0, moveSpots.Length);
    64.                 waitTime = startWaitTime;
    65.                 nav.SetDestination(moveSpots[randomSpot].position);
    66.             }
    67.             else
    68.             {
    69.                 waitTime -= Time.deltaTime;
    70.                 anim.SetInteger("condition", 1);
    71.             }
    72.         }
    73.     }
    74.  
    75.     public void ChasePlayer()
    76.     {
    77.         if (distance <= chaseRadius && distance > distToPlayer)
    78.         {
    79.             nav.SetDestination(player.transform.position);
    80.         }
    81.         else if (distance <= chaseRadius && distance < distToPlayer)
    82.         {
    83.                 nav.enabled = false;
    84.                 sandu.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationY;
    85.                 PlayerControls.SetActive(false);
    86.                 playercam.SetActive(false);
    87.                 killcam.SetActive(true);
    88.                 anim.SetInteger("condition", 3);
    89.                 StartCoroutine(ActivateMenu());
    90.         }
    91.     }
    92.  
    93.     IEnumerator ActivateMenu()
    94.     {
    95.         yield return new WaitForSeconds(2);
    96.         MainMenu.SetActive(true);
    97.     }
    98. }
    99.  

    So, sandu is the killer that is patroling, and the player transform is my player. I have tried modifying the if like this :

    Code (CSharp):
    1.         if (distance <= chaseRadius && distance > distToPlayer && sandu.position.y ==  player.position.y)
    2.         {
    3.             nav.SetDestination(player.transform.position);
    4.         }
    But if I do that then it just breaks and doesn't work at all anymore.How could I go about this?

    Thanks for the help!