ChangeModelTransMaterial.cs 4.7 KB

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