So my enemy navigation script doesn’t seem to work, or want to work at all.
Here’s the rundown of how it works.
- The Enemy detects if there are locations/waypoints to go to.
- They then go to that location.
- Once reaching the location, they then repeat the previous two steps.
- Once reaching all locations, they then backtrack to the locations that they already passed, and keep on going to them all until they reach the destination that they first went to, and then they turn around and rinse and repeat.
- If they detect the player with the correct tag on it, they start chasing the player.
The problem lies with the fact that when they detect the player in their first loop of navigation, after going through all the checkpoints, they don’t detect the player no matter, if I get close to them or not.
Below are the scripts that I’m using for the enemy navigation and detection, if anyone can help me find a solution, I would be very grateful.
Enemy Script
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.AI;
public class EnemyScript : MonoBehaviour
{
public NavMeshAgent agent;
public List<Transform> waypointPos = new List<Transform>();
public int index = 0;
public bool forward = true;
public float time;
public bool isPlayerInRange;
public Transform targetObject;
[SerializeField] EnemyRaycast enemyRaycast;
void Start()
{
agent.SetDestination(waypointPos[index].position);
}
// Sprinkle messages throughout my code, and try to fix the enemy location and the enemy raycast.
// Update is called once per frame
void Update()
{
if (DestinationReached())
{
SetNextWaypoint();
}
else if (isPlayerInRange == true)
{
Debug.Log("I am chasing");
ChasePlayer();
}
}
private void ChasePlayer()
{
transform.position = Vector3.MoveTowards(transform.position, targetObject.position, 10 * Time.deltaTime);
}
private bool DestinationReached()
{
float distance = Vector3.Distance(transform.position, waypointPos[index].position);
return distance < 1.7f;
}
private void SetNextWaypoint()
{
if (forward)
{
bool underwaypointCheck = index + 1 < waypointPos.Count;
index = (underwaypointCheck) ? index + 1 : index - 1;
if (!underwaypointCheck)
{
forward = false;
index++;
if (index == waypointPos.Count - 1)
{
forward = false;
if (isPlayerInRange == true)
{
ChasePlayer();
}
}
// Move Character
agent.SetDestination(waypointPos[index].position);
}
else
{
bool underwaypointCheck = index - 1 > 0;
index = (underwaypointCheck) ? index - 1 : 1;
if (!underwaypointCheck)
{
forward = true;
}
index--;
if (index == 0)
{
forward = true;
if (isPlayerInRange == true)
{
ChasePlayer();
}
}
// Move Character
agent.SetDestination(waypointPos[index].position);
}
}
}
Enemy Raycast Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EnemyRaycast : MonoBehaviour
{
public float range = 5f;
public LayerMask targetMask;
private EnemyScript enemyScript;
public GameObject player;
private void Awake()
{
enemyScript = transform.parent.GetComponent<EnemyScript>();
}
// Update is called once per frame
void Update()
{
Vector3 direction = transform.forward;
Ray theRay = new Ray(transform.position, direction);
Debug.DrawLine(transform.position, transform.position + direction * range);
if (Physics.Raycast(theRay, out RaycastHit hit, range))
{
Debug.Log("Hit Something " + hit.collider.name);
if (hit.collider.tag == "Player")
{
enemyScript.isPlayerInRange = true;
print("I see the Player, EVERYONE ATTACK!!!");
}
else if (hit.collider.tag == "Building")
{
print("Nothing going on, continue walking");
}
}
}
}