12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- using System.Collections.Generic;
- using UnityEngine;
- // Use this Script in parent folder of all Humans
- public class ChangeModelMaterial : MonoBehaviour
- {
- [Header("Highlighting Materials:")]
- public Material highlightRed;
- public Material highlightGreen;
- public Material highlightYellow;
- [Header("Thresholds:")]
- [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.")]
- public int thresholdGreen = 1;
- [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.")]
- public int thresholdYellow = 2;
-
- [Space]
- public float radius = 1f;
- private Vector3 center;
- private Collider[] hitHumans;
- private List<Component> allHumansTransform = new List<Component>(); // only first depth children of parent (Humans)
- // Start is called before the first frame update
- private void Start()
- {
- Component[] allTransform = GetComponentsInChildren<Transform>(); // all possible children of parent object
- foreach (Transform child in allTransform)
- {
- if (child.parent == allTransform[0])
- {
- allHumansTransform.Add(child);
- }
- }
- }
- //private void OnDrawGizmos()
- //{
- // Gizmos.color = Color.red;
- // Gizmos.DrawWireSphere(center, radius);
- //}
- private void FixedUpdate()
- {
- foreach (Component lst in allHumansTransform)
- {
- center = lst.GetComponent<Transform>().position;
- // only collisions with Layer "Humans" are listed in "hitHumans"
- hitHumans = Physics.OverlapSphere(center, radius, LayerMask.GetMask("Humans"));
- // if Human hits lesser then thresholdYellow but is hitting minimum 1 other human, then Material change to Green
- if (hitHumans.Length <= thresholdGreen)
- {
- Debug.Log(lst.gameObject.name + " set to Green");
- lst.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightGreen;
- }
- // if Human hits lesser then thresholdYellow but is hitting minimum 1 other human, then Material change to Yellow
- else if (hitHumans.Length <= thresholdYellow)
- {
- Debug.Log(lst.gameObject.name + " set to Yellow");
- lst.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightYellow;
- }
- else
- {
- // if Humans hits somebody else (threshold >= 1), then Material change to Red
- foreach (Collider hitHuman in hitHumans)
- {
- if (lst.gameObject.name != hitHuman.gameObject.name) // overlap with themselves
- {
- if (hitHuman.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material != highlightRed
- || lst.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material != highlightRed)
- {
- hitHuman.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightRed;
- lst.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightRed;
- }
- }
- }
- }
- }
- }
- }
|