using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class JubayerAi : MonoBehaviour
{
float distance;
public AudioSource jubuWarning;
Transform nearestWayPoint;
public float JubuWarningTime;
public float jubuWalkDuration;
public float searchDuration;
public Transform RandomPoint;
public Transform[] wayPoints;
public Animator anim;
public float ChaseSpeed;
public float WalkSpeed;
public float sightDistance;
public float JubuSpawnRate;
public bool isChasing, isSearching, isWalking, isInSight;
public bool Spawn;
public NavMeshAgent Jubayer;
public GameObject Player;
public GameObject JubayerObj;
public GameObject JubayerWhole;
void AlertPresence()
{
jubuWarning.Play();
}
public void StopChase()
{
isInSight = false;
isChasing = false;
isWalking = false;
isSearching = true;
}
void JubuChase()
{
if (isChasing)
{
if (Jubayer != null)
{
anim.SetTrigger("chase");
Jubayer.SetDestination(Player.transform.position);
Jubayer.speed = ChaseSpeed;
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player" && Player.GetComponent<PlayerMove>().isHiding == true)
{
StopChase();
}
}
void Jumpscare()
{
}
IEnumerator JubuSearch()
{
anim.SetTrigger("search");
Jubayer.speed = 0;
yield return new WaitForSeconds(searchDuration);
isSearching = false;
isWalking = true;
}
IEnumerator JubuWalk()
{
anim.SetTrigger("walk");
Jubayer.speed = WalkSpeed;
Jubayer.SetDestination(RandomPoint.position);
yield return new WaitForSeconds(3);
isWalking = false;
JubayerObj.SetActive(false);
JubayerWhole.transform.position = new Vector3(100, 100, 100);
Spawn = true;
Jubayer.enabled = false;
StartCoroutine(JubuSpawner());
}
void JubuDie()
{
anim.SetTrigger("die");
Destroy(this);
}
void Start()
{
Jubayer.enabled = false;
JubayerWhole.transform.position = new Vector3(500, 100, 100);
JubayerObj.SetActive(false);
Spawn = true;
StartCoroutine(JubuSpawner());
}
private void Update()
{
float shortestDistance = float.MaxValue;
NavMeshPath path = new NavMeshPath();
for (int i = 0; i < wayPoints.Length; i++)
{
if (NavMesh.CalculatePath(Player.transform.position, wayPoints[i].position, NavMesh.AllAreas, path))
{
distance = 0;
if (path.corners.Length > 0)
{
distance = Vector3.Distance(Player.transform.position, path.corners[0]);
for (int j = 1; j < path.corners.Length; j++)
{
distance += Vector3.Distance(path.corners[j - 1], path.corners[j]);
}
if (distance < shortestDistance)
{
shortestDistance = distance;
nearestWayPoint = wayPoints[i];
Debug.DrawLine(Player.transform.position, wayPoints[i].position, Color.red);
}
}
}
else
{
Debug.DrawLine(Player.transform.position, wayPoints[i].position);
}
}
Vector3 direction = (Player.transform.position - transform.position).normalized;
RaycastHit hit;
if (Physics.Raycast(transform.position, direction, out hit, sightDistance))
{
if (hit.collider.gameObject.tag == "Player")
{
isInSight = true;
}
}
else
{
isInSight = false;
}
if (isInSight && Player.GetComponent<PlayerMove>().isHiding == false)
{
isChasing = true;
}
if (isChasing == true)
{
isSearching = false;
isWalking = false;
JubuChase();
}
if (isSearching == true)
{
isChasing = false;
isWalking = false;
StartCoroutine(JubuSearch());
}
if (isWalking == true)
{
isChasing = false;
isSearching = false;
StartCoroutine(JubuWalk());
}
}
IEnumerator JubuSpawner()
{
if (Spawn)
{
yield return new WaitForSeconds(JubuSpawnRate);
AlertPresence();
yield return new WaitForSeconds(JubuWarningTime);
Spawn = false;
JubayerWhole.transform.position = nearestWayPoint.position;
Jubayer.enabled = true;
JubayerObj.SetActive(true);
isChasing = true;
}
JubuSpawner();
}
}
i know its big and long and messy but i’m a noob tho. when i start the game with jubayer(the gameobject where the script is attached to) on it works fine. it spawns in the nearest waypoint to the player but when i turn it off and then start the game and then some milliseconds or seconds if i turn jubayer on it will for some reason not be able to do this line( if (NavMesh.CalculatePath(Player.transform.position, wayPoints[i].position, NavMesh.AllAreas, path))) and be stuck there forever. the waypoints aren’t children to jubayer. help would be appreciated by alot… i’ve been added a debug log before the line if (NavMesh.CalculatePath(Player.transform.position, wayPoints[i].position, NavMesh.AllAreas, path)) it was only stuck on displaying the debug log lol. if you don’t wanna read such long code then it’s okay.