ChangeModelMaterial.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. // Use this Script in parent folder of all Humans
  4. public class ChangeModelMaterial : MonoBehaviour
  5. {
  6. [Header("Highlighting Materials:")]
  7. public Material highlightRed;
  8. public Material highlightGreen;
  9. public Material highlightYellow;
  10. [Header("Thresholds:")]
  11. [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.")]
  12. public int thresholdGreen = 1;
  13. [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.")]
  14. public int thresholdYellow = 2;
  15. [Space]
  16. public float radius = 1f;
  17. private Vector3 center;
  18. private Collider[] hitHumans;
  19. private List<Component> allHumansTransform = new List<Component>(); // only first depth children of parent (Humans)
  20. // Start is called before the first frame update
  21. private void Start()
  22. {
  23. Component[] allTransform = GetComponentsInChildren<Transform>(); // all possible children of parent object
  24. foreach (Transform child in allTransform)
  25. {
  26. if (child.parent == allTransform[0])
  27. {
  28. allHumansTransform.Add(child);
  29. }
  30. }
  31. }
  32. //private void OnDrawGizmos()
  33. //{
  34. // Gizmos.color = Color.red;
  35. // Gizmos.DrawWireSphere(center, radius);
  36. //}
  37. private void FixedUpdate()
  38. {
  39. foreach (Component lst in allHumansTransform)
  40. {
  41. center = lst.GetComponent<Transform>().position;
  42. // only collisions with Layer "Humans" are listed in "hitHumans"
  43. hitHumans = Physics.OverlapSphere(center, radius, LayerMask.GetMask("Humans"));
  44. // if Human hits lesser then thresholdYellow but is hitting minimum 1 other human, then Material change to Green
  45. if (hitHumans.Length <= thresholdGreen)
  46. {
  47. Debug.Log(lst.gameObject.name + " set to Green");
  48. lst.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightGreen;
  49. }
  50. // if Human hits lesser then thresholdYellow but is hitting minimum 1 other human, then Material change to Yellow
  51. else if (hitHumans.Length <= thresholdYellow)
  52. {
  53. Debug.Log(lst.gameObject.name + " set to Yellow");
  54. lst.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightYellow;
  55. }
  56. else
  57. {
  58. // if Humans hits somebody else (threshold >= 1), then Material change to Red
  59. foreach (Collider hitHuman in hitHumans)
  60. {
  61. if (lst.gameObject.name != hitHuman.gameObject.name) // overlap with themselves
  62. {
  63. if (hitHuman.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material != highlightRed
  64. || lst.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material != highlightRed)
  65. {
  66. hitHuman.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightRed;
  67. lst.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightRed;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }