ChangeMaterial.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections.Generic;
  2. using UnityEngine.AI;
  3. using UnityEngine;
  4. // Use this Script in parent folder of all Humans
  5. public class ChangeMaterial : MonoBehaviour
  6. {
  7. [Multiline]
  8. public string info;
  9. [Header("Highlighting Materials:")]
  10. public Material highlightRed;
  11. public Material highlightGreen;
  12. public Material highlightYellow;
  13. [Header("Highlighting Transparent Materials:")]
  14. public Material highlightTransRed;
  15. public Material highlightTransGreen;
  16. public Material highlightTransYellow;
  17. [Header("Thresholds:")]
  18. [Tooltip("The maximum number of people (including the observed) that will be colored green.\n Threshold Green = 1 means that no further person is allowed to approach.")]
  19. public int thresholdGreen = 1;
  20. [Tooltip("The maximum number of people (including the observed) that will be colored yellow.\n Threshold Yellow = 2 means that only one other person may approach.")]
  21. public int thresholdYellow = 2;
  22. [Space]
  23. public float radius = 1f;
  24. private Vector3 center;
  25. private Collider[] hitHumans;
  26. private List<Component> allHumansTransform = new List<Component>(); // only first depth children of parent (Humans)
  27. // Start is called before the first frame update
  28. private void Start()
  29. {
  30. Component[] allTransform = GetComponentsInChildren<Transform>(); // all possible children of parent object
  31. foreach (Transform child in allTransform)
  32. {
  33. if (child.parent == allTransform[0])
  34. {
  35. allHumansTransform.Add(child);
  36. }
  37. }
  38. }
  39. //private void OnDrawGizmos()
  40. //{
  41. // foreach (Component lst in allHumansTransform)
  42. // {
  43. // Gizmos.color = Color.blue;
  44. // Gizmos.DrawWireSphere(lst.transform.position, radius);
  45. // }
  46. //}
  47. private void FixedUpdate()
  48. {
  49. foreach (Component lst in allHumansTransform)
  50. {
  51. center = lst.GetComponent<Transform>().position;
  52. // only collisions with Layer "Humans" are listed in "hitHumans"
  53. hitHumans = Physics.OverlapSphere(center, radius, LayerMask.GetMask("Humans"));
  54. bool differentTag = false;
  55. foreach (Collider hitHuman in hitHumans)
  56. {
  57. if (lst.CompareTag(hitHuman.tag))
  58. differentTag = false;
  59. else
  60. {
  61. differentTag = true;
  62. break;
  63. }
  64. }
  65. int hitHumansLength = 0;
  66. foreach (Collider hitHuman in hitHumans)
  67. {
  68. if (lst.CompareTag(hitHuman.tag))
  69. hitHumansLength++;
  70. }
  71. // if Human hits lesser then thresholdYellow but is hitting minimum 1 other human, then Material change to Green
  72. if (hitHumansLength <= thresholdGreen)
  73. {
  74. if (!differentTag)
  75. {
  76. lst.GetComponent<NavMeshAgent>().radius = 0.5F;
  77. Debug.Log(lst.gameObject.name + " set to Green");
  78. lst.transform.Find("BasicMotionsDummyModel").Find("DummyMesh").gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightGreen;
  79. }
  80. else
  81. {
  82. lst.GetComponent<NavMeshAgent>().radius = 0.0001F;
  83. Debug.Log(lst.gameObject.name + " set to Trans Green");
  84. lst.transform.Find("BasicMotionsDummyModel").Find("DummyMesh").gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightTransGreen;
  85. }
  86. }
  87. // if Human hits lesser then thresholdYellow but is hitting minimum 1 other human, then Material change to Yellow
  88. else if (hitHumansLength <= thresholdYellow)
  89. {
  90. if (!differentTag)
  91. {
  92. lst.GetComponent<NavMeshAgent>().radius = 0.5F;
  93. Debug.Log(lst.gameObject.name + " set to Yellow");
  94. lst.transform.Find("BasicMotionsDummyModel").Find("DummyMesh").gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightYellow;
  95. }
  96. else
  97. {
  98. lst.GetComponent<NavMeshAgent>().radius = 0.0001F;
  99. Debug.Log(lst.gameObject.name + " set to Trans Yellow");
  100. lst.transform.Find("BasicMotionsDummyModel").Find("DummyMesh").gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightTransYellow;
  101. }
  102. }
  103. // everything else change to Red
  104. else
  105. {
  106. if (!differentTag)
  107. {
  108. lst.GetComponent<NavMeshAgent>().radius = 0.5F;
  109. Debug.Log(lst.gameObject.name + " set to Red");
  110. lst.transform.Find("BasicMotionsDummyModel").Find("DummyMesh").gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightRed;
  111. }
  112. else
  113. {
  114. lst.GetComponent<NavMeshAgent>().radius = 0.0001F;
  115. Debug.Log(lst.gameObject.name + " set to Trans Red");
  116. lst.transform.Find("BasicMotionsDummyModel").Find("DummyMesh").gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightTransRed;
  117. }
  118. }
  119. }
  120. }
  121. }