// When an agent wants to help another, it has to check if it reachs this other agent private void CheckReachPersonToHelp() { if (agent.enabled && !agent.pathPending) { // we check if the agent is near of the agent to help but not at the exact position (we don't want a colision) if (CalculateDistanceSquare(gameObject.transform.position, new Vector3(xDest, 0.0f, zDest)) <= 1.0f) { int index = ClosestDoorIndex(); xDest = EvacuationDest[index].x; zDest = EvacuationDest[index].y; agent.ResetPath(); agent.SetDestination(new Vector3(xDest, this.gameObject.transform.position.y, zDest)); isGoingToHelp = false; // security but should never happend if (personHelped != null) { AgentEvacuation script = personHelped.GetComponent <AgentEvacuation>(); script.hasCalledHelp = false; script.xDest = xDest; script.zDest = zDest; script.animator.SetBool("isInPanic", false); script.animator.Rebind(); script.animator.SetBool("isRunning", true); script.obstacle.enabled = false; script.agent.enabled = true; script.agent.SetDestination(new Vector3(script.xDest, script.gameObject.transform.position.y, script.zDest)); } } } }
private void MoveAway() { float minDist = float.MaxValue; float tmp; xDest = Random.Range(-10.7f, 9.7f); zDest = Random.Range(-6.7f, 6.7f); foreach (GameObject ag in AllAgents.agents) { tmp = AgentEvacuation.CalculateDistanceSquare(ag.transform.position, new Vector3(xDest, 0.0f, zDest)); if (tmp < minDist) { minDist = tmp; } } //distance must be greater than 1.5f if (minDist >= 2.25f) { isSeekingForDistance = false; obstacle.enabled = false; agent.enabled = true; agent.SetDestination(new Vector3(xDest, this.gameObject.transform.position.y, zDest)); currentState = States.GOINGAWAY; animator.SetBool("isWaiting", false); animator.SetBool("isEating", false); animator.SetBool("isTalking", false); animator.Rebind(); animator.SetBool("isWalking", true); } }
private void CheckReachPerson() { if (agent.enabled) { if (!agent.pathPending) { if (AgentEvacuation.CalculateDistanceSquare(gameObject.transform.position, personToTalk.transform.position) <= 1.0f) { animator.SetBool("isWalking", false); animator.Rebind(); animator.SetBool("isTalking", true); agent.enabled = false; obstacle.enabled = true; currentState = States.TALKING; Vector3 relativePos = personToTalk.transform.position - this.gameObject.transform.position; Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up); this.gameObject.transform.rotation = rotation; // if this agent is the first to reach the person who wants to talk, it triggers the state and animation if (personToTalk.GetComponent <AgentManager>().currentState != States.TALKING) { Vector3 rPos = this.gameObject.transform.position - personToTalk.transform.position; Quaternion rot = Quaternion.LookRotation(rPos, Vector3.up); personToTalk.transform.rotation = rot; personToTalk.GetComponent <AgentManager>().animator.SetBool("isWaiting", false); personToTalk.GetComponent <AgentManager>().animator.Rebind(); personToTalk.GetComponent <AgentManager>().animator.SetBool("isTalking", true); personToTalk.GetComponent <AgentManager>().currentState = States.TALKING; foreach (GameObject ag in personToTalk.GetComponent <AgentManager>().talkers) { ag.GetComponent <AgentManager>().talkers.Add(personToTalk); foreach (GameObject ag2 in personToTalk.GetComponent <AgentManager>().talkers) { if (ag != ag2) { ag.GetComponent <AgentManager>().talkers.Add(ag2); } } } } } } } if ((personToTalk.GetComponent <AgentManager>().currentState != States.TALKING) && (personToTalk.GetComponent <AgentManager>().currentState != States.WANTTOTALK)) { foreach (GameObject ag in talkers) { ag.GetComponent <AgentManager>().talkers.Remove(this.gameObject); } talkers.Clear(); animator.SetBool("isWalking", false); animator.Rebind(); animator.SetBool("isWaiting", true); agent.enabled = false; obstacle.enabled = true; currentState = States.WAITING; } }
// declenched by catching event "onAlarm", turn off this script and turn on the AgentEvacuation script to manage evacuation // give some useful variables to AgentEvacuation script as well public void ReactToAlarm() { AgentEvacuation nextScript = GetComponent <AgentEvacuation>(); nextScript.enabled = true; nextScript.SetCurrentState(currentState); nextScript.SetHunger(hunger); nextScript.SetShyness(shyness); nextScript.SetMoveRate(moveRate); this.enabled = false; }
public void Play() { // we reset the static variables (it's useful when we restart the simulation) AgentSpawn.maxAgentNbr = nbrAgents; DoorPassing.isEvacuation = false; AgentManager.ResetOnTalk(); AgentEvacuation.ResetOnAskHelp(); TriggerAlarm.ResetOnAlarm(); TriggerAlarm.ResetOnClose(); TriggerAlarm.nbrAgentsInRoom = AgentSpawn.maxAgentNbr; SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); }
// Choose the closest table private int ClosestTableIndex() { int Index = 0; float MinDistance = float.MaxValue; float tmp; for (int i = 0; i < TableNbr; i++) { tmp = AgentEvacuation.CalculateDistanceSquare(gameObject.transform.position, new Vector3(Tables[i].x, 0.0f, Tables[i].y)); if (MinDistance > tmp) { MinDistance = tmp; Index = i; } } return(Index); }