123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System.Collections.Generic;
- using UnityEngine;
- public class ChangeCapsuleMaterial : MonoBehaviour
- {
- public Material highlightRed;
- public Material highlightGreen;
- public Material highlightYellow;
- public int threshold = 1;
- public float radius = 1f;
- private Vector3 center;
- private Collider[] hitHumans;
- private List<Component> allHumansTransform = new List<Component>();
-
- private void Start()
- {
- Component[] allTransform = GetComponentsInChildren<Transform>();
- foreach (Transform child in allTransform)
- {
- if (child.parent == allTransform[0])
- {
- allHumansTransform.Add(child);
- }
- }
-
-
-
-
- }
- private void Update()
- {
- foreach (Component lst in allHumansTransform)
- {
- center = lst.GetComponent<Transform>().position;
-
- hitHumans = Physics.OverlapSphere(center, radius, LayerMask.GetMask("Humans"));
-
-
- if (hitHumans.Length <= 1)
- {
- Debug.Log(lst.gameObject.name + " set to Green");
- lst.GetComponent<MeshRenderer>().material = highlightGreen;
- }
-
- else if (hitHumans.Length <= threshold)
- {
- Debug.Log(lst.gameObject.name + " set to Yellow");
- lst.transform.GetChild(0).GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().material = highlightYellow;
- }
- else
- {
-
- foreach (Collider hitHuman in hitHumans)
- {
- if (lst.gameObject.name != hitHuman.gameObject.name)
- {
- if (hitHuman.GetComponent<MeshRenderer>().material != highlightRed
- || lst.GetComponent<MeshRenderer>().material != highlightRed)
- {
- Debug.Log(lst.gameObject.name + " overlaps with " + hitHuman.gameObject.name);
- hitHuman.GetComponent<MeshRenderer>().material = highlightRed;
- lst.GetComponent<MeshRenderer>().material = highlightRed;
- }
- }
- }
- }
- }
- }
- }
|