I have updated to unity 5, but my AI doesn’t follow my player.There was no problem before updated.
I use this
static var player : GameObject;
var target : Transform;
var agent : NavMeshAgent;
function Start(){
target = GameObject.FindWithTag("Player").transform;
player = GameObject.Find("Player");
agent = character.GetComponent.<NavMeshAgent>();
}
I make distance between player and AI. The AI follows my player if I place my player in scene near AI. But when it is far, I make it stop. But then it doesn’t follow anymore when I get close.
I use this for stopping it.
Okay so I did a little bit of reading about the stop command for the navmeshagent since I didn’t know a whole lot about it. So you might need:
agent.Resume();
then the agent might continue to the last location that was set so you might need to update the location to travel to.
Not sure if this will work but you could try it.
agent = character.GetComponent.<NavMeshAgent>();
has to be ,
agent = GetComponent(NavMeshAgent); // Put this line in awake();
// if character is referenced in your case then
agent = character.GetComponent(NavMeshAgent);
And in Update use the following ,
agent.destination= target.position;
agent.destination = targets*.position; // For following multiple waypoints where targets is an array of transforms.*
// var targets : Transform[]; *
// If you are using SetDestination , it requires a Vector3 ,
agent.SetDestination(target.position); // is Incorrect
I do not know how AI works in Unity lower than version 5.0, but yesterday I had the same problem. I rewrited standart AICharacterController script and it is working fine now. My code:
public class AICharacterControl : MonoBehaviour
{
public NavMeshAgent agent { get; private set; } // the navmesh agent required for the path finding
public ThirdPersonCharacter character { get; private set; } // the character we are controlling
public Transform target; // target to aim for
private Vector3 _oldPos;
private Transform ethanPosition;
private bool _moveFlag = true;
// Use this for initialization
private void Start()
{
// get the components on the object we need ( should not be null due to require component so no need to check )
agent = GetComponentInChildren<NavMeshAgent>();
character = GetComponent<ThirdPersonCharacter>();
ethanPosition = GameObject.Find("EthanPosition").transform;
agent.updateRotation = false;
agent.updatePosition = true;
}
// Update is called once per frame
private void Update()
{
if (Vector3.Distance(ethanPosition.position, target.position) >= agent.stoppingDistance && _oldPos != target.position)
{
_moveFlag = true;
_oldPos = target.position;
agent.SetDestination(target.position);
}
if (Vector3.Distance(ethanPosition.position, target.position) < agent.stoppingDistance && _moveFlag)
{
_moveFlag = false;
agent.SetDestination(ethanPosition.position);
}
if (target != null && _moveFlag)
{
// use the values to move the character
character.Move(agent.desiredVelocity, false, false);
}
else
{
//We still need to call the character's move function, but we send zeroed input as the move param.
character.Move(Vector3.zero, false, false);
}
}
}