Answer the question
In order to leave comments, you need to log in
How to solve the problem in a different way (Trigger)?
Good evening everyone. When solving the problem of creating a scope for an object and finding the one closest to the object in view, I had some difficulties in optimization. Please, who gets along well with Unity in C#, please help.
So, here is the description of the task from my point of view in Unity-C#: We have an object that we manage and it has an empty child object that has 4 components (Transform, Rigidbody, Sphere Collider and Detector script) in Sphere Collider - Is Trigger = true. Further, in the Detector script, in the OnTriggerEnter event, I add an object to the list of objects when entering the collider field, in the OnTriggerStay event, I find among all the object that is in the collider field the closest to my object and write down the name, then through the Find method of the GameObject class, I find by the name of this very object and change the color of the material to green so that the player knows what object it is aimed at, but for now I have not been able to solve this subtask that is highlighted (when we changed the color of the object to green, then we need to return its original color if the object is not the closest one ), and when the object leaves the collider, I remove it from the list of all objects.
Help me solve this subtask (when we change the color of an object to green, then we need to return its original color if the object is not the closest one).
If you have any ideas on how to optimize this entire task, I will be very happy.
Thank you for your time.
Detector script code
/// <summary>
/// Список объектов в поле зрения
/// </summary>
private List<GameObject> visibleObject = new List<GameObject>();
/// <summary>
/// Старое и новое расстояние
/// </summary>
private float Next = 0, Old = 0;
/// <summary>
/// Имя объекта
/// </summary>
public static string nameObj = "";
/// <summary>
/// Вошёл в поле зрения
/// </summary>
/// <param name="other"></param>
private void OnTriggerEnter(Collider other)
{
if ((!other.name.Equals(Connect.NamePlayer)) && (!other.name.Equals("Terrain")) /*&& (!other.name.Contains("object"))*/) // если не равно нашему имени и имени объекта и поля
{
GameObject tempObject = other.gameObject; // инициализируем новый объект
if ((visibleObject.Find(n => !n.name.Equals(tempObject.name))) || (visibleObject.Count == 0)) // если такого объекта нет в списке или список пуст
{
visibleObject.Add(tempObject); // добавляем в список объект который находится в поле зрения
}
}
}
/// <summary>
/// Находится в поле зрения
/// </summary>
/// <param name="other"></param>
private void OnTriggerStay(Collider other)
{
if ((!other.name.Equals(Connect.NamePlayer)) && (!other.name.Equals("Terrain")) /*&& (!other.name.Contains("object"))*/) // если не равно нашему имени и имени объекта и поля
{
Old = (int)Math.Sqrt(Math.Pow((visibleObject[0].transform.position.x - transform.position.x), 2) + Math.Pow((visibleObject[0].transform.position.z - transform.position.z), 2)); // нахождение расстояния между указателем мыши и персонажем
foreach (GameObject obj in visibleObject)
{
Next = (int)Math.Sqrt(Math.Pow((obj.transform.position.x - transform.position.x), 2) + Math.Pow((obj.transform.position.z - transform.position.z), 2)); // нахождение расстояния между указателем мыши и персонажем
DebugConsole.Log(Next + " " + visibleObject.Count);
if (Next < Old || visibleObject.Count == 1) // если следующее расстояние меньше чем старое
{
Old = Next;
nameObj = obj.name;
}
}
GameObject.Find(nameObj).GetComponent<Renderer>().sharedMaterial.SetColor("_EmissionColor", Color.green);
}
}
/// <summary>
/// Вышел из поле зрения
/// </summary>
/// <param name="other"></param>
private void OnTriggerExit(Collider other)
{
visibleObject.Remove(visibleObject.Find(n => n.name == other.name)); // удаляем из списка объект который вышел из поля зрения
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question