UPDATE: Still not working. This is what I did so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AgentControl : MonoBehaviour
{
public List<Transform> points = new List<Transform>();
public bool isInMeeting = false;
public float threshold = 5f;
public float strength = 5f;
private float speed;
private int destPoint = 0;
private NavMeshAgent agent;
private NavMeshAgent[] agents;
void Start()
{
agents = FindObjectsOfType<NavMeshAgent>();
agent = GetComponent<NavMeshAgent>();
var agentsDestionations = GameObject.FindGameObjectsWithTag("Waypoint");
for (int i = 0; i < agentsDestionations.Length; i++)
{
points.Add(agentsDestionations[i].transform);
}
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
agent.autoBraking = true;
agent.speed = Random.Range(10, 50);
speed = agent.speed;
GotoNextPoint();
}
void GotoNextPoint()
{
// Returns if no points have been set up
if (points.Count == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Count;
}
void Update()
{
Meeting();
// Choose the next destination point when the agent gets
// close to the current one.
if (isInMeeting == false && !agent.pathPending && agent.remainingDistance < 2f)
{
GotoNextPoint();
}
}
private void Meeting()
{
for (int i = 0; i < agents.Length; i++)
{
if (agent != agents[i])
{
float dist = Vector3.Distance(transform.position, agents[i].transform.position);
if ( dist < threshold)
{
agents[i].GetComponent<AgentControl>().isInMeeting = true;
isInMeeting = true;
agent.GetComponent<Renderer>().material.SetColor("_Color", Color.yellow);
agents[i].GetComponent<Renderer>().material.SetColor("_Color", Color.yellow);
StartCoroutine(Meeting(agents[i]));
break;
}
}
}
}
IEnumerator Meeting(NavMeshAgent Agent)
{
var targetRotation = Quaternion.LookRotation(Agent.transform.position - agent.transform.position);
var str = Mathf.Min(strength * Time.deltaTime, 1);
agent.transform.rotation = Quaternion.Lerp(agent.transform.rotation, targetRotation, str);
yield return new WaitForSeconds(10);
Agent.GetComponent<AgentControl>().isInMeeting = false;
isInMeeting = false;
}
}
The script is attached to a Prefab and then I duplicate the prefab so I have many agents. So each agent have the script attached.
The logic as I see it should be that each two or more agents that the distance is less the threshold should stop rotating smooth facing each other waiting 10 seconds rotate back and continue each one on the way they was.
A meeting can be between two or more agents. And if an agent is on the way and there is already a meeting he can join the existing meeting or maybe not to join and keep moving on his way the logic is to create some kind of meetings or more to say groups of conversations.
So when the game is running you see some groups of agents in conversations some agents will not be at any conversation and some agents will leave/join existing conversations.
But I'm still can't make the first part a simple meeting between two agents. When two agents getting close less then threshold they are in yellow but not stopping then other agents getting close and get in yellow too. In the end there is a bog group of agents all the agents at the same place like a one big conversation (meeting).