I don’t know why it isn’t working, the navmesh ai stuff is fine, but the raycast stuff is broken. sorry if it’s obvious, I’m very new to coding
my script:
public class Sight : MonoBehaviour
{
public Camera Eyes;
Transform target;
NavMeshAgent Agent;
private bool Yes;
public float range;
private void Start()
{
target = PlayerManager.instance.player.transform;
Agent = GetComponent<NavMeshAgent>();
}
private void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.tag == "Player")
{
Yes = true;
}
}
private void OnTriggerExit(Collider collider)
{
if (collider.gameObject.tag == "Player")
{
Yes = false;
}
}
private void Update()
{
if (Yes == true)
{
RaycastHit hit;
Vector3 origin = transform.position;
Vector3 direction = Eyes.transform.forward;
Ray ray = new Ray(origin, direction);
if (Physics.Raycast(origin, direction, out hit, range))
{
if (hit.collider.gameObject.tag == "Player")
{
Agent.SetDestination(target.position);
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= Agent.stoppingDistance)
{
FaceTarget();
}
}
}
}
}
void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookrotation = Quaternion.LookRotation(new Vector3(direction.x,
0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookrotation,
Time.deltaTime * 5f);
}
}