EnemyPatrol vs. AIThirdPersonController

I am trying to get a model to patrol an area. I started out with the standard asset AIThirdPersonController and was able to replace that with the other model. The Script for it (AI Character Control) only seems to allow for a single target. I tried deleting that script and replacing it with Enemy Patrol, this allows me to add all my targets and does not throw any console errors, but the character does not move.


  • Jonathan Gonzalez(jgonzalez) replied

    The AI Third Person Controller is a bit more complex than just that one script. That script may just provide a target to move towards, but it also has to ensure that it'll animate and move the character to that position too so it's using it's movements scripts to do that. I would recommend taking a look at what it's doing in that original script when it has a target to move towards. You can reference those same methods in your own scripts. 

  • Jonathan Gonzalez(jgonzalez) replied

    So I looked at the script myself and it does indeed reference the character controller to move. You could copy that entire script and modify it yourself. It has the basic concepts of what you want, you just need to modify it slightly so that instead of stopping after one specific target it then repeats the process to go to another target. 

  • resin213 replied

    Thanks for the quick reply. I had a feeling it might be something like that. I was surprised Unity Answers weren't more helpful to me. I tried looking at the scripts side by side, and attempted to identify what was happening in each section. I guess this will be a little bit of trial and error for me, but I do have some idea of what to take from the enemypatrol script to adapt the AIThirdPersonController script.

  • resin213 replied

    I don't know where to go next, I tried doing this with the script, but I'm kind of shooting in the dark here, might just have to set it on the backburner and come back to it when I have a little more experience to go on:


    using System;
    using UnityEngine;

    namespace UnityStandardAssets.Characters.ThirdPerson
    {
    [RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))]
    [RequireComponent(typeof (ThirdPersonCharacter))]
    public class AICharacterControl : MonoBehaviour
    {
    public UnityEngine.AI.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[] points;
    private int destPoint = 0;
    private Animator anim;


    private void Start()
    {
    // get the components on the object we need ( should not be null due to require component so no need to check )
    anim = GetComponent<Animator> ();
    agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
    character = GetComponent<ThirdPersonCharacter>();
    destPoint = UnityEngine.Random.Range (0, points.Length);
    NextPoint ();
            agent.updateRotation = false;
            agent.updatePosition = true;
     
    }

    void NextPoint() {

    if (points.Length == 0)
    return;

    destPoint = UnityEngine.Random.Range (0, points.Length);

    agent.destination = points[destPoint].position;

    destPoint = (destPoint + 1) % points.Length;
    }


    private void Update()
    {
    if (agent.isStopped) {
    anim.SetFloat ("Speed_f", 0);
    return;
    }

    anim.SetFloat ("Speed_f", 1f);

    if (!agent.pathPending && agent.remainingDistance < 0.5f) {
    NextPoint ();

    }

    }

    void OnAnimatorMove()
    {
    agent.speed = (anim.deltaPosition / Time.deltaTime).magnitude;
    }


    }
    }