i am working on an enemy for my game that follows you until it doesnt see you anymore.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class pathfindingPlayer : MonoBehaviour
{
RaycastHit hit;
NavMeshAgent _navMeshAgent;
void Awake() => _navMeshAgent = GetComponent<NavMeshAgent>();
public bool seen;
public float noticeDistance = 7f;
void Update()
{
Debug.DrawRay(transform.position, transform.forward * noticeDistance, Color.red);
Ray PlayerRay = new Ray(transform.position, Vector3.back);
if(Physics.Raycast(PlayerRay, out RaycastHit hitInfo, noticeDistance))
{
if (hitInfo.collider.CompareTag("Player"))
{
Vector3 SeenPlayer = hitInfo.point;
_navMeshAgent.SetDestination(SeenPlayer);
}
}
}
}
Now the problem is that there is only one Raycast being shot out. Therefore the enemy only runs in one direction. Is there a way to add multiple raycasts to this or do i need to rewrite the code? Thanks in advance
CodePudding user response:
You can make a function that makes raycasts and call it every x seconds, but for what you want to achieve i would try putting a circle collider on the enemy with the radius of how far the enemy can see and make sure to set the collider as trigger, then make it go towards the player if the player triggers the collider