Search Unity

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

Problem using setActive(false)

Discussion in 'Scripting' started by GCMSA, Apr 19, 2017.

  1. GCMSA

    GCMSA

    Joined:
    Mar 29, 2017
    Posts:
    16
    Hi everyone,

    It's been few days I'm confused with an issue that's why I need the help from unity's community.

    I have an array of gameobjects and when the first component of the array is desactivated (using SetActive function) the position Vector3 and the rotation Vector3 of the next component of the array changes.

    Could you please help me to solve the problem?

    Thank you
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Post your code
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Turning off a gameobject shouldn't effect the rest of the gameobjects in the array, which means you're doing something else to them most likely. As @StarManta said. Post some code.
     
  4. GCMSA

    GCMSA

    Joined:
    Mar 29, 2017
    Posts:
    16
    Thank you for the quick reply

    This is my code

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7.  
    8. [System.Serializable]
    9. public class emergencyGroupProfile
    10. {
    11.     [Tooltip("Emergency group name")]
    12.     public string name;
    13.  
    14.     [Tooltip("Emergency group description")]
    15.     public string description;
    16.  
    17.     [Tooltip("Assistants")]
    18.     public List<GameObject> assistants = new List<GameObject>();
    19.  
    20.     [Tooltip("Patients in need of assistance (e.g. people with reduced mobility)")]
    21.     public List<GameObject> assistedPatients = new List<GameObject>();
    22.  
    23.     [Header("Triage approach")]
    24.     [Tooltip("No priority - Agents are assisted considering the distance to the attendants")]
    25.     public bool dstToAttendant = false;
    26.  
    27.     [Tooltip("Agents in immediate danger are evacuated first, followed by those requiring the minimum assistance and then the agent with higher dependency level")]
    28.     public bool levelOfDependency = false;
    29.  
    30.     [Tooltip("Priority is specified by the user")]
    31.     public bool userDefined = false;
    32.  
    33.     [Tooltip("Triage order if defined by the user")]
    34.     public List<GameObject> triageOrder = new List<GameObject>();
    35.  
    36. }
    37.  
    38.  
    39.  
    40. public class EmergencyGroupsClass : MonoBehaviour
    41. {
    42.     public emergencyGroupProfile emergencyGroupProfile;
    43.     public GameObject staffMembers;
    44.  
    45.     private GameObject[] AttachmentPoints;
    46.     private float[] preparationTimes;
    47.     private bool gatheringPhaseFinished = false;
    48.     private bool attachmentPhaseFinished = false;
    49.     private float[] DistancesToAttachmentPoint;
    50.     private NavMeshAgent[] AssistantsNavMeshAgents;
    51.     private CapsuleCollider[] AssistantsCapsuleColliders;
    52.     private BoxCollider[] patientsBoxColliders;
    53.     private int index = 0;
    54.  
    55.  
    56.     //The path to follow during the movement phase
    57.     public EditorPathScript PathToFollow;
    58.     public int CurrentWayPointID = 0;
    59.     private float arriveDistance = 0.5f;
    60.     //public string pathName;
    61.  
    62.     private Vector3 lastPosition;
    63.     private Vector3 currentPosition;
    64.  
    65.     //
    66.     public GameObject nearestExit;
    67.  
    68.     private float dist;
    69.     private float farthestDist = 0;
    70.     private GameObject farthestAgent = null;
    71.     private GameObject farthestAgentAttachmentPoint = null;
    72.  
    73.     void Awake()
    74.     {
    75.        
    76.         preparationTimes = new float[emergencyGroupProfile.triageOrder.Count];
    77.  
    78.         AssistantsNavMeshAgents = new NavMeshAgent[emergencyGroupProfile.assistants.Count];
    79.  
    80.         AssistantsCapsuleColliders = new CapsuleCollider[AssistantsNavMeshAgents.Length];
    81.  
    82.         patientsBoxColliders = new BoxCollider[emergencyGroupProfile.assistedPatients.Count];
    83.  
    84.  
    85.  
    86.  
    87.         for (int i = 0; i < emergencyGroupProfile.triageOrder.Count; i++)
    88.             preparationTimes[i] = emergencyGroupProfile.triageOrder[i].GetComponent<Patient>().preparationTime;
    89.  
    90.         for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    91.         {
    92.             AssistantsNavMeshAgents[i] = emergencyGroupProfile.assistants[i].GetComponent<NavMeshAgent>();
    93.         }
    94.     }
    95.  
    96.  
    97.     void Update()
    98.     {
    99.  
    100.         //Debug.Log(emergencyGroupProfile.triageOrder.Count);
    101.  
    102.         if (index < emergencyGroupProfile.triageOrder.Count)
    103.         {
    104.             //Debug.Log(index);
    105.             PathToFollow = emergencyGroupProfile.triageOrder[index].GetComponent<Patient>().PathToFollow;
    106.             AttachmentPoints = new GameObject[emergencyGroupProfile.triageOrder[index].GetComponent<Patient>().attachmentPoints.Length];
    107.             AttachmentPoints = emergencyGroupProfile.triageOrder[index].GetComponent<Patient>().attachmentPoints;
    108.  
    109.             DistancesToAttachmentPoint = new float[AttachmentPoints.Length];
    110.  
    111.             if (!gatheringPhaseFinished)
    112.             {
    113.                 gatheringPhase();
    114.             }
    115.             else if (!attachmentPhaseFinished)
    116.             {
    117.                 attachmentPhase();
    118.  
    119.             }
    120.             else
    121.             {
    122.                 //preparation phase
    123.                 StartCoroutine(preparationPhase());
    124.             }
    125.         }
    126.  
    127.         /*else
    128.         {
    129.  
    130.             for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    131.             {
    132.                 float Distance = Vector3.Distance(AssistantsNavMeshAgents[i].transform.position, nearestExit.transform.position);
    133.                 AssistantsNavMeshAgents[i].destination = nearestExit.transform.position;
    134.                 if (Distance < AssistantsNavMeshAgents[i].stoppingDistance + 0.01f)
    135.                 {
    136.                     AssistantsNavMeshAgents[i].gameObject.SetActive(false);
    137.                     Debug.Log(AssistantsNavMeshAgents[i].gameObject.name + " Exit time = " + Time.realtimeSinceStartup + " s");
    138.                 }
    139.             }
    140.             return;
    141.         }*/
    142.  
    143.     }
    144.  
    145.     private bool gatheringPhase()
    146.     {
    147.         //Debug.Log("Gathering Phase started");
    148.         //float dist;
    149.  
    150.         //float farthestDist = 0;
    151.         //GameObject farthestAgent = null;
    152.         //GameObject farthestAgentAttachmentPoint = null;
    153.        
    154.  
    155.         bool rotationFinished = false;
    156.  
    157.         for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    158.         {
    159.             DistancesToAttachmentPoint[i] = Vector3.Distance(AssistantsNavMeshAgents[i].transform.position, AttachmentPoints[i].transform.position);
    160.             dist = DistancesToAttachmentPoint[i];
    161.  
    162.             if (dist > farthestDist)
    163.             {
    164.                 farthestDist = dist;
    165.                 farthestAgent = AssistantsNavMeshAgents[i].gameObject;
    166.                 farthestAgentAttachmentPoint = AttachmentPoints[i].gameObject;
    167.                 //Debug.Log(farthestDist);
    168.  
    169.             }
    170.  
    171.             AssistantsNavMeshAgents[i].destination = AttachmentPoints[i].transform.position;
    172.         }
    173.  
    174.         //Debug.Log(farthestAgent.name);
    175.  
    176.         for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    177.         {
    178.  
    179.             if (HasArrived(AssistantsNavMeshAgents[i]))
    180.             {
    181.                 emergencyGroupProfile.assistants[i].transform.rotation = Quaternion.RotateTowards(emergencyGroupProfile.assistants[i].transform.rotation,
    182.                 AttachmentPoints[i].transform.rotation, AssistantsNavMeshAgents[i].angularSpeed * Time.deltaTime);
    183.                                              
    184.             }
    185.         }
    186.  
    187.  
    188.      
    189.  
    190.         for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    191.         {
    192.             float fartestAgentLocalAngle;
    193.             fartestAgentLocalAngle = farthestAgent.gameObject.transform.localEulerAngles.y;
    194.  
    195.             float fartestAgentAttachmentPointLocalAngle;
    196.             fartestAgentAttachmentPointLocalAngle = farthestAgentAttachmentPoint.gameObject.transform.parent.gameObject.transform.eulerAngles.y;
    197.  
    198.             //Debug.Log(dist);
    199.             if (dist <= AssistantsNavMeshAgents[i].stoppingDistance + 0.01f)
    200.             {
    201.                 Debug.Log(farthestAgent.gameObject.transform.localEulerAngles.y);
    202.                 Debug.Log(farthestAgentAttachmentPoint.gameObject.transform.parent.transform.eulerAngles.y);
    203.                
    204.  
    205.  
    206.                 if (fartestAgentLocalAngle == fartestAgentAttachmentPointLocalAngle || fartestAgentLocalAngle == fartestAgentAttachmentPointLocalAngle)
    207.                 {
    208.                     //Debug.Log("Gathering Phase Finished");
    209.  
    210.                     rotationFinished = true;
    211.                    
    212.                 }
    213.             }
    214.         }
    215.  
    216.         Debug.Log(rotationFinished);
    217.  
    218.         if (rotationFinished)
    219.         {
    220.             return gatheringPhaseFinished = true;
    221.         }
    222.  
    223.         return gatheringPhaseFinished = false;
    224.     }
    225.  
    226.     private bool attachmentPhase()
    227.     {
    228.  
    229.         for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    230.         {
    231.             emergencyGroupProfile.assistants[i].transform.parent = AttachmentPoints[i].transform  ;
    232.             //emergencyGroupProfile.assistants[i].transform.position = AttachmentPoints[i].transform.position;
    233.             //emergencyGroupProfile.assistants[i].transform.rotation = AttachmentPoints[i].transform.rotation;
    234.            
    235.             AssistantsNavMeshAgents[i].enabled = false;
    236.  
    237.         }
    238.  
    239.         //Debug.Log("Attachment Phase Finished");
    240.  
    241.  
    242.         return attachmentPhaseFinished = true;
    243.  
    244.  
    245.     }
    246.  
    247.     private IEnumerator preparationPhase()
    248.     {
    249.         //Debug.Log(index);
    250.         //Debug.Log("Preparation Phase Started");
    251.         yield return new WaitForSeconds(emergencyGroupProfile.triageOrder[index].GetComponent<Patient>().preparationTime);
    252.         //movement phase
    253.         movementPhase();
    254.  
    255.        
    256.     }
    257.  
    258.  
    259.     private void movementPhase()
    260.     {
    261.         //emergencyGroupProfile.triageOrder[index].GetComponent<NavMeshObstacle>().enabled = false;
    262.        // emergencyGroupProfile.triageOrder[index].GetComponent<NavMeshAgent>().enabled = true;
    263.  
    264.         //Debug.Log("Mouvement Phase Started");
    265.  
    266.         lastPosition = emergencyGroupProfile.triageOrder[index].gameObject.transform.position;
    267.         float distance = Vector3.Distance(PathToFollow.pathObjs[CurrentWayPointID].position, emergencyGroupProfile.triageOrder[index].gameObject.transform.position);
    268.         emergencyGroupProfile.triageOrder[index].gameObject.transform.position = Vector3.MoveTowards(emergencyGroupProfile.triageOrder[index].gameObject.transform.position, PathToFollow.pathObjs[CurrentWayPointID].position, Time.deltaTime * emergencyGroupProfile.triageOrder[index].gameObject.GetComponent<NavMeshAgent>().speed);
    269.         var rotation = Quaternion.LookRotation(PathToFollow.pathObjs[CurrentWayPointID].position - emergencyGroupProfile.triageOrder[index].transform.position);
    270.         emergencyGroupProfile.triageOrder[index].gameObject.transform.rotation = Quaternion.Slerp(emergencyGroupProfile.triageOrder[index].gameObject.transform.rotation, rotation, Time.deltaTime * emergencyGroupProfile.triageOrder[index].gameObject.GetComponent<NavMeshAgent>().angularSpeed);
    271.  
    272.         if (distance <= arriveDistance)
    273.         {
    274.             CurrentWayPointID++;
    275.         }
    276.  
    277.         if (CurrentWayPointID >= PathToFollow.pathObjs.Count)
    278.         {
    279.             //detach assistants
    280.             for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    281.             {
    282.                 emergencyGroupProfile.assistants[i].transform.parent = staffMembers.transform; //null;
    283.  
    284.                 //AssistantsNavMeshAgents[i].baseOffset = 1.11f;
    285.                 AssistantsNavMeshAgents[i].enabled = true;
    286.  
    287.             }
    288.  
    289.             emergencyGroupProfile.triageOrder[index].gameObject.SetActive(false);
    290.             Debug.Log(emergencyGroupProfile.triageOrder[index].gameObject.name + " Exit time = " + Time.realtimeSinceStartup + " s");
    291.             index++;
    292.  
    293.  
    294.             ResetValues();
    295.         }
    296.        
    297.  
    298.  
    299.     }
    300.  
    301.     protected bool HasArrived(NavMeshAgent navMeshAgent)
    302.     {
    303.         // The path hasn't been computed yet if the path is pending.
    304.         float remainingDistance;
    305.         if (navMeshAgent.pathPending)
    306.         {
    307.             remainingDistance = float.PositiveInfinity;
    308.         }
    309.         else
    310.         {
    311.             remainingDistance = navMeshAgent.remainingDistance;
    312.         }
    313.  
    314.         return remainingDistance <= navMeshAgent.stoppingDistance + 1.0f;
    315.     }
    316.  
    317.     private void ResetValues()
    318.     {
    319.         gatheringPhaseFinished = false;
    320.         attachmentPhaseFinished = false;
    321.         CurrentWayPointID = 0;
    322.         farthestDist = 0;
    323.         farthestAgent = null;
    324.         farthestAgentAttachmentPoint = null;
    325.     }
    326.  
    327.  
    328. }
    329.  
    330.  
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    If your issue is happening with the code that is at line 289, I'd suggest you investigate line 282 - changing parentage could easily affect positions and rotations. transform.SetParent gives you a little more control over what happens there, maybe try that?
     
  6. GCMSA

    GCMSA

    Joined:
    Mar 29, 2017
    Posts:
    16
    Thank you StarManta. I changed the line but it doesn't solve the problem. Please check the attached pictures to understand the problem.

    I have three bedriden patients that should be assisted by able bodied agents.
    Fig1 : shows the first patient aided (everything works as it should)
    Fig2 : shows the transform of the second patient before that the first agent is disabled (setactive stuff)
    Fig3: shows the transform of the second patient after the first agent is disabled
    and Fig4 : shows what is going wrong (the third agent joinning the group is not having the correct position and rotation)

     

    Attached Files:

    • Fig1.PNG
      Fig1.PNG
      File size:
      111.1 KB
      Views:
      780
    • Fig2.PNG
      Fig2.PNG
      File size:
      307.6 KB
      Views:
      816
    • Fig3.PNG
      Fig3.PNG
      File size:
      678.1 KB
      Views:
      860
    • Fig4.PNG
      Fig4.PNG
      File size:
      62.9 KB
      Views:
      833
  7. GCMSA

    GCMSA

    Joined:
    Mar 29, 2017
    Posts:
    16
    Any other suggestion from the community?

    Thank you
     
  8. jmoris

    jmoris

    Joined:
    Sep 28, 2013
    Posts:
    9
    I agree with StarManta, in line 282 you're assigning as parent something that is not even initialized (you say null, but it's actually only declared). Maybe if you describe a bit more which variable in the code corresponds to which object in the scene it would be easier to followup... Anyway, it seems to me a parental problem.
     
  9. GCMSA

    GCMSA

    Joined:
    Mar 29, 2017
    Posts:
    16
    Please find attached a screenshot of the hierarchy.

    SGEnvironment is the play environment.

    Staff members is an empty GameObject that holds the assistants' characters. Each Assistant has 4 components: its Transform, a capsule collider, a navmeshagent and a rigidbody.

    Dependent patients is an empty GameObject that holds the patients to assist. Each patient has 6 components (see the attached picture : PatientComponents). The attached script called Patient is as follow:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.  
    6.  
    7. public class Patient : MonoBehaviour
    8. {
    9.  
    10.     [Header("Physical profile")]
    11.     [Tooltip("Patient walking speed in m/s")]
    12.     [Range(0, 10)]
    13.     public float patientSpeed = 1;
    14.  
    15.  
    16.     //patient preparation time
    17.     public float preparationTime;
    18.     //patient attachment points
    19.     public GameObject[] attachmentPoints;
    20.  
    21.  
    22.     public EditorPathScript PathToFollow;
    23. }
    24.  
    25.  
    Emergency Groups is an empty Gameobject which holds EmergencyGroups (composed of multiple assistants). For example EG2 (see the third picture attached) is composed of 4 assistants, and has to assist 3 patients with a specific triage order.

    The EmergencyGroupClass script is as follow :

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7.  
    8. [System.Serializable]
    9. public class emergencyGroupProfile
    10. {
    11.     [Tooltip("Emergency group name")]
    12.     public string name;
    13.  
    14.     [Tooltip("Emergency group description")]
    15.     public string description;
    16.  
    17.     [Tooltip("Assistants")]
    18.     public List<GameObject> assistants = new List<GameObject>();
    19.  
    20.     [Tooltip("Patients in need of assistance (e.g. people with reduced mobility)")]
    21.     public List<GameObject> assistedPatients = new List<GameObject>();
    22.  
    23.     [Header("Triage approach")]
    24.     [Tooltip("No priority - Agents are assisted considering the distance to the attendants")]
    25.     public bool dstToAttendant = false;
    26.  
    27.     [Tooltip("Agents in immediate danger are evacuated first, followed by those requiring the minimum assistance and then the agent with higher dependency level")]
    28.     public bool levelOfDependency = false;
    29.  
    30.     [Tooltip("Priority is specified by the user")]
    31.     public bool userDefined = false;
    32.  
    33.     [Tooltip("Triage order if defined by the user")]
    34.     public List<GameObject> triageOrder = new List<GameObject>();
    35.  
    36. }
    37.  
    38.  
    39.  
    40. public class EmergencyGroupsClass : MonoBehaviour
    41. {
    42.     public emergencyGroupProfile emergencyGroupProfile;
    43.     public GameObject staffMembers = null;
    44.  
    45.     private GameObject[] AttachmentPoints;
    46.     private float[] preparationTimes;
    47.     private bool gatheringPhaseFinished = false;
    48.     private bool attachmentPhaseFinished = false;
    49.     private float[] DistancesToAttachmentPoint;
    50.     private NavMeshAgent[] AssistantsNavMeshAgents;
    51.     private NavMeshPath[] AssistantsNavMeshPaths;
    52.     private CapsuleCollider[] AssistantsCapsuleColliders;
    53.     private BoxCollider[] patientsBoxColliders;
    54.     private int index = 0;
    55.  
    56.  
    57.     //The path to follow during the movement phase
    58.     public EditorPathScript PathToFollow;
    59.     public int CurrentWayPointID = 0;
    60.     private float arriveDistance = 0.5f;
    61.     //public string pathName;
    62.  
    63.     private Vector3 lastPosition;
    64.     private Vector3 currentPosition;
    65.  
    66.     //
    67.     public GameObject nearestExit;
    68.  
    69.     private float dist;
    70.     private float timeTakenByAssistants;
    71.     private float farthestDist = 0f;
    72.     private float longestTime = 0f;
    73.     private GameObject farthestAgent = null;
    74.     private GameObject farthestAgentAttachmentPoint = null;
    75.  
    76.     private Vector3 NextPatientInitialPosition;
    77.     private Quaternion NextPatientInitialRotation;
    78.  
    79.     void Awake()
    80.     {
    81.        
    82.         preparationTimes = new float[emergencyGroupProfile.triageOrder.Count];
    83.  
    84.         AssistantsNavMeshAgents = new NavMeshAgent[emergencyGroupProfile.assistants.Count];
    85.  
    86.         AssistantsNavMeshPaths = new NavMeshPath[emergencyGroupProfile.assistants.Count];
    87.  
    88.         AssistantsCapsuleColliders = new CapsuleCollider[AssistantsNavMeshAgents.Length];
    89.  
    90.         patientsBoxColliders = new BoxCollider[emergencyGroupProfile.assistedPatients.Count];
    91.  
    92.  
    93.  
    94.  
    95.         for (int i = 0; i < emergencyGroupProfile.triageOrder.Count; i++)
    96.             preparationTimes[i] = emergencyGroupProfile.triageOrder[i].GetComponent<Patient>().preparationTime;
    97.  
    98.         for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    99.         {
    100.             AssistantsNavMeshAgents[i] = emergencyGroupProfile.assistants[i].GetComponent<NavMeshAgent>();
    101.             AssistantsNavMeshPaths[i] = new NavMeshPath();
    102.         }
    103.     }
    104.  
    105.  
    106.     void Update()
    107.     {
    108.  
    109.         //Debug.Log(emergencyGroupProfile.triageOrder.Count);
    110.  
    111.         if (index < emergencyGroupProfile.triageOrder.Count)
    112.         {
    113.             //Debug.Log(index);
    114.             PathToFollow = emergencyGroupProfile.triageOrder[index].GetComponent<Patient>().PathToFollow;
    115.             AttachmentPoints = new GameObject[emergencyGroupProfile.triageOrder[index].GetComponent<Patient>().attachmentPoints.Length];
    116.             AttachmentPoints = emergencyGroupProfile.triageOrder[index].GetComponent<Patient>().attachmentPoints;
    117.  
    118.             DistancesToAttachmentPoint = new float[AttachmentPoints.Length];
    119.  
    120.             if (!gatheringPhaseFinished)
    121.             {
    122.                 gatheringPhase();
    123.             }
    124.             else if (!attachmentPhaseFinished)
    125.             {
    126.                 attachmentPhase();
    127.  
    128.             }
    129.             else
    130.             {
    131.                 //preparation phase
    132.                 StartCoroutine(preparationPhase());
    133.             }
    134.         }
    135.  
    136.         /*else
    137.         {
    138.  
    139.             for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    140.             {
    141.                 float Distance = Vector3.Distance(AssistantsNavMeshAgents[i].transform.position, nearestExit.transform.position);
    142.                 AssistantsNavMeshAgents[i].destination = nearestExit.transform.position;
    143.                 if (Distance < AssistantsNavMeshAgents[i].stoppingDistance + 0.01f)
    144.                 {
    145.                     AssistantsNavMeshAgents[i].gameObject.SetActive(false);
    146.                     Debug.Log(AssistantsNavMeshAgents[i].gameObject.name + " Exit time = " + Time.realtimeSinceStartup + " s");
    147.                 }
    148.             }
    149.             return;
    150.         }*/
    151.  
    152.     }
    153.  
    154.     private bool gatheringPhase()
    155.     {
    156.         //Debug.Log("Gathering Phase started");
    157.         //float dist;
    158.  
    159.         //float farthestDist = 0;
    160.         //GameObject farthestAgent = null;
    161.         //GameObject farthestAgentAttachmentPoint = null;
    162.         NextPatientInitialPosition=emergencyGroupProfile.triageOrder[index+1].transform.position;
    163.         NextPatientInitialRotation = emergencyGroupProfile.triageOrder[index + 1].transform.rotation;
    164.  
    165.         for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    166.         {
    167.             if (AssistantsNavMeshAgents[i].enabled)
    168.             {
    169.                 AssistantsNavMeshAgents[i].CalculatePath(AttachmentPoints[i].transform.position, AssistantsNavMeshPaths[i]);
    170.             }
    171.  
    172.             emergencyGroupProfile.assistants[i].GetComponent<Collider>().enabled = true;
    173.         }
    174.  
    175.         bool rotationFinished = false;
    176.  
    177.         for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    178.         {
    179.             //DistancesToAttachmentPoint[i] = AssistantsNavMeshAgents[i].remainingDistance;
    180.             DistancesToAttachmentPoint[i] = CalculatePathLength(AttachmentPoints[i].transform.position, AssistantsNavMeshPaths[i]);
    181.             //DistancesToAttachmentPoint[i] = Vector3.Distance(AssistantsNavMeshAgents[i].transform.position, AttachmentPoints[i].transform.position);
    182.             dist = DistancesToAttachmentPoint[i];
    183.             timeTakenByAssistants = DistancesToAttachmentPoint[i] / AssistantsNavMeshAgents[i].speed;
    184.  
    185.             if (timeTakenByAssistants > longestTime)
    186.             {
    187.                 farthestDist = dist;
    188.                 longestTime = timeTakenByAssistants;
    189.                 farthestAgent = AssistantsNavMeshAgents[i].gameObject;
    190.                 farthestAgentAttachmentPoint = AttachmentPoints[i].gameObject;
    191.                 //Debug.Log(farthestDist);
    192.  
    193.             }
    194.  
    195.             Debug.Log(farthestAgent.name);
    196.  
    197.             AssistantsNavMeshAgents[i].destination = AttachmentPoints[i].transform.position;
    198.         }
    199.  
    200.        
    201.  
    202.         for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    203.         {
    204.  
    205.             if (HasArrived(AssistantsNavMeshAgents[i]))
    206.             {
    207.                 emergencyGroupProfile.assistants[i].transform.rotation = Quaternion.RotateTowards(emergencyGroupProfile.assistants[i].transform.rotation,
    208.                 AttachmentPoints[i].transform.rotation, AssistantsNavMeshAgents[i].angularSpeed * Time.deltaTime);
    209.                                              
    210.             }
    211.         }
    212.  
    213.  
    214.      
    215.  
    216.         for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    217.         {
    218.             float fartestAgentLocalAngle;
    219.             fartestAgentLocalAngle = farthestAgent.gameObject.transform.localEulerAngles.y;
    220.  
    221.             float fartestAgentAttachmentPointLocalAngle;
    222.             fartestAgentAttachmentPointLocalAngle = farthestAgentAttachmentPoint.gameObject.transform.parent.gameObject.transform.eulerAngles.y;
    223.  
    224.             //Debug.Log(dist);
    225.             dist = Vector3.Distance(AssistantsNavMeshAgents[i].transform.position, AttachmentPoints[i].transform.position);
    226.  
    227.             if (dist <= AssistantsNavMeshAgents[i].stoppingDistance + 0.1f)
    228.             {
    229.                 //Debug.Log(farthestAgent.gameObject.transform.localEulerAngles.y);
    230.                 //Debug.Log(farthestAgentAttachmentPoint.gameObject.transform.parent.transform.eulerAngles.y);
    231.  
    232.                 float anglesDiff = Mathf.Abs(fartestAgentLocalAngle) - Mathf.Abs(fartestAgentAttachmentPointLocalAngle);
    233.  
    234.                 //Debug.Log(anglesDiff);
    235.  
    236.                 if (Mathf.Abs(fartestAgentLocalAngle - fartestAgentAttachmentPointLocalAngle) <= 1f )
    237.                 {
    238.                     Debug.Log("Gathering Phase Finished");
    239.  
    240.                     rotationFinished = true;
    241.                    
    242.                 }
    243.             }
    244.         }
    245.  
    246.         //Debug.Log(rotationFinished);
    247.  
    248.         if (rotationFinished)
    249.         {
    250.             return gatheringPhaseFinished = true;
    251.         }
    252.  
    253.         return gatheringPhaseFinished = false;
    254.     }
    255.  
    256.     private bool attachmentPhase()
    257.     {
    258.  
    259.         for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    260.         {
    261.             emergencyGroupProfile.assistants[i].transform.parent = AttachmentPoints[i].transform  ;
    262.             //emergencyGroupProfile.assistants[i].transform.position = AttachmentPoints[i].transform.position;
    263.             //emergencyGroupProfile.assistants[i].transform.rotation = AttachmentPoints[i].transform.rotation;
    264.            
    265.             AssistantsNavMeshAgents[i].enabled = false;
    266.  
    267.         }
    268.  
    269.         //Debug.Log("Attachment Phase Finished");
    270.  
    271.  
    272.         return attachmentPhaseFinished = true;
    273.  
    274.  
    275.     }
    276.  
    277.     private IEnumerator preparationPhase()
    278.     {
    279.         //Debug.Log(index);
    280.         //Debug.Log("Preparation Phase Started");
    281.         yield return new WaitForSeconds(emergencyGroupProfile.triageOrder[index].GetComponent<Patient>().preparationTime);
    282.         //movement phase
    283.         movementPhase();
    284.  
    285.        
    286.     }
    287.  
    288.  
    289.     private void movementPhase()      
    290.     {
    291.         //emergencyGroupProfile.triageOrder[index].GetComponent<NavMeshObstacle>().enabled = false;
    292.         // emergencyGroupProfile.triageOrder[index].GetComponent<NavMeshAgent>().enabled = true;
    293.  
    294.         //Debug.Log("Mouvement Phase Started");
    295.  
    296.         for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    297.         {
    298.             emergencyGroupProfile.assistants[i].GetComponent<Collider>().enabled = false;
    299.         }
    300.  
    301.         lastPosition = emergencyGroupProfile.triageOrder[index].gameObject.transform.position;
    302.         float distance = Vector3.Distance(PathToFollow.pathObjs[CurrentWayPointID].position, emergencyGroupProfile.triageOrder[index].gameObject.transform.position);
    303.         emergencyGroupProfile.triageOrder[index].gameObject.transform.position = Vector3.MoveTowards(emergencyGroupProfile.triageOrder[index].gameObject.transform.position, PathToFollow.pathObjs[CurrentWayPointID].position, Time.deltaTime * emergencyGroupProfile.triageOrder[index].gameObject.GetComponent<NavMeshAgent>().speed);
    304.         var rotation = Quaternion.LookRotation(PathToFollow.pathObjs[CurrentWayPointID].position - emergencyGroupProfile.triageOrder[index].transform.position);
    305.         emergencyGroupProfile.triageOrder[index].gameObject.transform.rotation = Quaternion.Slerp(emergencyGroupProfile.triageOrder[index].gameObject.transform.rotation, rotation, Time.deltaTime * emergencyGroupProfile.triageOrder[index].gameObject.GetComponent<NavMeshAgent>().angularSpeed);
    306.  
    307.         if (distance <= arriveDistance)
    308.         {
    309.             CurrentWayPointID++;
    310.         }
    311.  
    312.         if (CurrentWayPointID >= PathToFollow.pathObjs.Count)
    313.         {
    314.             //detach assistants
    315.             for (int i = 0; i < emergencyGroupProfile.assistants.Count; i++)
    316.             {
    317.                 emergencyGroupProfile.assistants[i].transform.SetParent(staffMembers.transform); //null;
    318.  
    319.                 //AssistantsNavMeshAgents[i].baseOffset = 1.11f;
    320.                 AssistantsNavMeshAgents[i].enabled = true;
    321.  
    322.             }
    323.  
    324.             emergencyGroupProfile.triageOrder[index].gameObject.SetActive(false);
    325.             Debug.Log(emergencyGroupProfile.triageOrder[index].gameObject.name + " Exit time = " + Time.realtimeSinceStartup + " s");
    326.  
    327.             index++; //or //emergencyGroupProfile.triageOrder.Remove(emergencyGroupProfile.triageOrder[index].gameObject);
    328.  
    329.             ResetValues();
    330.  
    331.  
    332.  
    333.         }
    334.        
    335.  
    336.  
    337.     }
    338.  
    339.     protected bool HasArrived(NavMeshAgent navMeshAgent)
    340.     {
    341.         // The path hasn't been computed yet if the path is pending.
    342.         float remainingDistance;
    343.         if (navMeshAgent.pathPending)
    344.         {
    345.             remainingDistance = float.PositiveInfinity;
    346.         }
    347.         else
    348.         {
    349.             remainingDistance = navMeshAgent.remainingDistance;
    350.         }
    351.  
    352.         return remainingDistance <= navMeshAgent.stoppingDistance + 1.0f;
    353.     }
    354.  
    355.     private void ResetValues()
    356.     {
    357.         gatheringPhaseFinished = false;
    358.         attachmentPhaseFinished = false;
    359.         CurrentWayPointID = 0;
    360.         dist = 0;
    361.         farthestDist = 0;
    362.         timeTakenByAssistants = 0f;
    363.         longestTime = 0f;
    364.         farthestAgent = null;
    365.         farthestAgentAttachmentPoint = null;
    366.  
    367.         //emergencyGroupProfile.triageOrder[index].transform.position = NextPatientInitialPosition;
    368.         //emergencyGroupProfile.triageOrder[index].transform.rotation = NextPatientInitialRotation;
    369.  
    370.  
    371.     }
    372.  
    373.     private float CalculatePathLength(Vector3 targetPosition, NavMeshPath path)
    374.     {
    375.         float pathLength = 0f;
    376.  
    377.  
    378.         Vector3[] allWayPoints = new Vector3[path.corners.Length + 2];
    379.  
    380.         allWayPoints[0] = gameObject.transform.position + new Vector3(0 ,1.1f,0);
    381.         allWayPoints[allWayPoints.Length - 1] = targetPosition + new Vector3(0, 1.1f, 0);
    382.        
    383.         //Debug.Log(path.corners.Length);
    384.         //Debug.Log(allWayPoints.Length);
    385.  
    386.         for (int i = 0; i < path.corners.Length; i++)
    387.         {
    388.             allWayPoints[i + 1] = path.corners[i];
    389.         }
    390.  
    391.         for (int i = 0; i < allWayPoints.Length - 1; i++)
    392.         {
    393.             //Debug.Log(allWayPoints[i]);
    394.             pathLength += Vector3.Distance(allWayPoints[i], allWayPoints[i + 1]);
    395.         }
    396.  
    397.         return pathLength;
    398.     }
    399.  
    400.  
    401. }
    402.  
    403.  
    I hope it is clear now. And I'm sorry if I'm making mistakes because I'm a bit new on programming.

    Thank you for your help.
     

    Attached Files:

  10. GCMSA

    GCMSA

    Joined:
    Mar 29, 2017
    Posts:
    16
    Any help from the community?

    Thank you