public static IEnumerator TrailLeader(AIBlackboard blackboard, ComponentBlackboard components, float speed, int bufferIndex) { blackboard.Waiting = false; components.StopShootingPlayer(); TrailingFollower target = TrailingFollower.SpawnTrailer(blackboard.targetBuffer[bufferIndex]); NavMeshAgent navigation = components.navigation; MecanimLocomotion locomotion = components.locomotion; Transform aiTransform = components.aiObject.transform; navigation.SetDestination(target.Waypoint.position); locomotion.speed = 8; locomotion.lookWhereMoving = true; locomotion.turn = false; locomotion.move = true; yield return(null); while (Vector3.Distance(target.Waypoint.position, aiTransform.position) > 5) { navigation.SetDestination(target.Waypoint.position); components.Navigate(); target.PositionWaypoint(); yield return(new WaitForSeconds(0.1f)); } locomotion.speed = 3; locomotion.lookWhereMoving = false; locomotion.turn = true; locomotion.move = true; yield return(null); while (true) { if (Vector3.Distance(target.Waypoint.position, aiTransform.position) > 1) { locomotion.lookDirection = -locomotion.moveDirection; navigation.SetDestination(target.Waypoint.position); target.PositionWaypoint(); components.Navigate(); } else { components.StopNavigating(); locomotion.turn = false; locomotion.move = false; } yield return(null); } }
public static TrailingFollower SpawnTrailer(Transform lead) { GameObject g = new GameObject("trailingFollower"); TrailingFollower tk = g.AddComponent <TrailingFollower>(); GameObject way = new GameObject("waypoint"); way.transform.parent = g.transform; Vector3[] positions = new Vector3[] { lead.transform.position - lead.transform.forward, lead.transform.position - lead.transform.forward, }; tk.Init(lead, way.transform, positions); return(tk); }