- Home /
Question by
bishamonn · Jan 19 at 12:14 AM ·
ontriggerenter
OnTriggerEnter problem
hi, I want the enemy to follow the player when he touches the ground, but he does not react when I see him touch the ground. I added the tags and collider too. What can I do ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class Enemy : MonoBehaviour
{
NavMeshAgent pathfinder;
Transform target;
// Start is called before the first frame update
void Start()
{
pathfinder = GetComponent<NavMeshAgent>();
target = GameObject.FindGameObjectWithTag("Player").transform;
Debug.Log("gg");
}
void OnTriggerEnter(Collider other)
{
//other = GetComponent<Collider>();
if (other.gameObject.tag=="floor" )
{
Debug.Log("aaaa");
StartCoroutine(UpdatePath());
pathfinder.SetDestination(target.position);
}
else
{
Debug.Log("no");
}
}
IEnumerator UpdatePath()
{
float refreshRate = .25f;
while (target != null)
{
Vector3 targetPosition = new Vector3(target.position.x, 0, target.position.z);
pathfinder.SetDestination(targetPosition);
yield return new WaitForSeconds(refreshRate);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
[SOLVED] OnTriggerExit only called when object moves 0 Answers
collider vs collider2d 1 Answer
whats wrong.... 0 Answers
OnTriggerEnter not firing 1 Answer
Ontriggerenter not working 1 Answer