- Home /
Make AI follow player without jumping about
Let me start by saying I've only been using Unity for about a day so if you see better ways to do what I've done in my (small) amount of code please say!
So I'm trying to get the enemy to follow the player around using the NavMesh. I thought that OnTriggerStay would be the answer but this obviously just resets SetDestination constantly. With OnTriggerEnter it saves the position but before doing that it jumps away from the player until the player is fully within the SphereCollider, before finally moving to the saved destination.
My main issue is how to stop the enemy jumping backwards when the player collider triggers the enemy collider with a secondary issue of how to have the position of the player update constantly whilst the player collider is within the enemy collider. Help on either of these would be greatly appreciated.
I've tried putting nav.SetDestination(target.position); within Awake() or Start() but it doesn't fix either issue.
Here's my code:
using UnityEngine;
using System.Collections;
public class enemyFollow : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
private NavMeshAgent nav;
NavMeshPath path;
void Awake(){
nav = GetComponent<NavMeshAgent>();
}
void OnTriggerEnter(Collider other){
if(other.attachedRigidbody){
nav.SetDestination(target.position);
nav.Move(target.position);
}
}
}