Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Adding multiple targets to AICharacterControl

Discussion in 'Navigation' started by Resin, Feb 27, 2018.

  1. Resin

    Resin

    Joined:
    May 3, 2017
    Posts:
    13
    I have been attempting to add multiple patrol destinations to the Standard Assets AICharacterControl
    (maybe this is a bad idea to begin with, idk I am very much a beginner with this).
    I tried Frankensteining code similar to this https://docs.unity3d.com/Manual/nav-AgentPatrol.html
    into the AICharacterControl, mostly because I was able to get AICharacterControl to work with the model I had downloaded but was not able to get the original patrol.cs to work.
    Anyway this is the faulty script that doesn't work that I came up with:

    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    5. namespace UnityStandardAssets.Characters.ThirdPerson
    6. {
    7.     [RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))]
    8.     [RequireComponent(typeof (ThirdPersonCharacter))]
    9.     public class AICharacterControl : MonoBehaviour
    10.     {
    11.         public UnityEngine.AI.NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
    12.         public ThirdPersonCharacter character { get; private set; } // the character we are controlling
    13.         public Transform[] points;
    14.         private int destPoint = 0;
    15.         private Animator anim;
    16.  
    17.  
    18.         private void Start()
    19.         {
    20.             // get the components on the object we need ( should not be null due to require component so no need to check )
    21.             anim = GetComponent<Animator> ();
    22.             agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
    23.             character = GetComponent<ThirdPersonCharacter>();
    24.             destPoint = UnityEngine.Random.Range (0, points.Length);
    25.             NextPoint ();
    26.             agent.updateRotation = false;
    27.             agent.updatePosition = true;
    28.            
    29.         }
    30.  
    31.         void NextPoint() {
    32.  
    33.             if (points.Length == 0)
    34.                 return;
    35.  
    36.             destPoint = UnityEngine.Random.Range (0, points.Length);
    37.  
    38.             agent.destination = points[destPoint].position;
    39.  
    40.             destPoint = (destPoint + 1) % points.Length;
    41.         }
    42.  
    43.  
    44.         private void Update()
    45.         {
    46.             if (agent.isStopped) {
    47.                 anim.SetFloat ("Speed_f", 0);
    48.                 return;
    49.             }
    50.  
    51.             anim.SetFloat ("Speed_f", 1f);
    52.  
    53.             if (!agent.pathPending && agent.remainingDistance < 0.5f) {
    54.                 NextPoint ();
    55.  
    56.             }
    57.  
    58.         }
    59.  
    60.         void OnAnimatorMove()
    61.         {
    62.             agent.speed = (anim.deltaPosition / Time.deltaTime).magnitude;
    63.         }
    64.  
    65.  
    66.     }
    67. }
    68.  
    Any help or direction to a better method is much appreciated.
     
    Last edited: Feb 27, 2018
  2. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    654
    Whats your current issue with this ? Can you put in code format ? icon next to save... Its tough to read as is..

    Code (CSharp):
    1. Using UnityEngine.AI;
    https://www.assetstore.unity3d.com/en/#!/content/5328
    Have a look at this unity tutorial. On navmesh and rootmotion.
     
unityunity