- Home /
Unity Pro Navigation AI c# animations and stopping?
So I am using Unity pro and I have a terrain with a navmesh bake and my monster follows the player, but of course he just runs right through him. Also, he is always chasing the player no matter how far away.
I was wondering how I can stop the enemy when he gets within a certain distance of my player (just simple colliders I suppose?).
More importantly I have walking and attacking animations and I was wondering how I can play those when he is moving and when is attacking. I know how to write the code for c# but I pretty confused on how or where to implement it.
Also, I had this working before on a different script, but now that I have the Pro version everything is different with AI. I had it so that the AI would check the player's flashlight component to see if it was enabled or not, and when it is, the enemy can start to follow from a farther distance, otherwise the enemy has to be much closer. I know how to write the codes for that, I think, but how can I get my enemy to NOT move based on an if statement? The AI follow that is below I found on an online tutorial but it doesnt really help as far as telling the enemy what to do when he has reached the player and etc.
So far I just have this to get the AI to follow:
using UnityEngine;
using System.Collections;
public class navmeshagentai : MonoBehaviour
{
public int distanceaway = 80;
public GameObject go;
public Transform m_Player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
GetComponent<NavMeshAgent>().destination = m_Player.position;
}
}
Answer by fafase · Aug 23, 2012 at 05:46 AM
Ok, add a Character Controller to your enemy and fetch the controller to move him with Move().
Look there:http://answers.unity3d.com/questions/229547/enemies-dont-collide.html
for your enemy to stop chasing you you have two possibilities:
1-Using Vector3.Distance(transform,transform) like so:
if(Vector3.Distance(transform.position,player.position)< range)Attack();
You might want to cut the AI with InvokeRepeating
var player:Transform;
var chase:boolean;
var range:int;
function Start(){
player=GameObject.Find("Player");
chase = false;
InvokeRepeating("CheckDistance",1.0,1.0);
}
function Update(){
if(chase)Attack();
}
function CheckDistance(){
if(Vector3.Distance(transform.position,player.position)<range)chase =true;
else chase =false;
}
This way you check only once every second.
2- Using a trigger sphere, add a sphere collider to your enemy prefab and tick IsTrigger. The radisu of the sphere determines the distance the enemy starts to chase:
var player:Transform;
var chase:boolean;
function Start(){
player=GameObject.Find("Player");
chase = false;
}
function Update(){
if(chase)Attack();
}
function OntriggerEnter(other:Collider){
if(other.gameObject.tag=="Player")chase=true;}
function OntriggerExit(other:Collider){
if(other.gameObject.tag=="Player")chase=false;}
it is all in UnityScript but C# works on the same principle.
Alright I see what you mean, I'll try this later today and let you know what works
I ended up doing something different but this helped me reach that conclusion so thank you
NO worries, how different? That could be interesting to know.