private void UserInteractWithActors(Vector2 point) { var ray =…
- User
private void UserInteractWithActors(Vector2 point) { var ray = Camera.main.ScreenPointToRay(point);
RaycastHit[] hits; hits = Physics.RaycastAll(ray, 100); var vaildRaycastHits = hits.Where(hit => hit.collider.GetComponent<CardDeckDisplayer>() || hit.collider.GetComponent<CardDisplayer>() || hit.collider.GetComponent<HeroDisplayer>() || hit.collider.GetComponent<HeroSkillDisplayer>()).OrderBy(hit => hit.distance).ToList().ToList(); if (vaildRaycastHits.Count == 0) { InteractNothing(); return; } var vaildCardDeckRayCastHit = vaildRaycastHits.Where(hit => hit.collider.GetComponent<CardDeckDisplayer>()).ToList(); if (vaildCardDeckRayCastHit.Count > 0) { var hit = vaildRaycastHits[0]; var cardDeck = hit.collider.GetComponent<CardDeckDisplayer>(); if (cardDeck) { InteractCardDeck(hit.collider); } else if (CurrentInteractedCardDeck != null) { CurrentInteractedCardDeck.Interested(false); CurrentInteractedCardDeck = null; } return; } var raycastHit = vaildRaycastHits[0]; Interact(raycastHit.collider); } 帮忙优化这段代码,实现raycastHit不是默认取第0个而是距离最近的 - Assistant
Sure! The original code is already attempting to find the closest hit by ordering the hits by distance, but there are some inefficiencies and redundancy. We can optimize the code by cleaning up the process, ensuring correct usage of distance-based selection, and removing extra calls. Here's a refined version:
- User
优化后的代码还是没有说是距离最近的啊, validRaycastHits要从这个数组里面找到距离最近的
- Assistant
The code already sorts
validRaycastHitsbased on distance usingOrderBy(hit => hit.distance). The first element of this sorted list is the closest hit. However, I understand if you want to explicitly find and highlight the closest hit. Here's how you could do this more explicitly:private void UserInteractWithActors(Vector2 point) { var ray = Camera.main.ScreenPointToRay(point); // Perform a raycast to find all hits up to a distance of 100 units RaycastHit[] hits = Physics.RaycastAll(ray, 100); // Filter valid hits and order by distance var validRaycastHits = hits .Where(hit => hit.collider.GetComponent<CardDeckDisplayer>() || hit.collider.GetComponent<CardDisplayer>() || hit.collider.GetComponent<HeroDisplayer>() || hit.collider.GetComponent<HeroSkillDisplayer>() ) .OrderBy(hit => hit.distance) .ToList(); // If no valid hits, interact with nothing if (validRaycastHits.Count == 0) { InteractNothing(); return; } // Explicitly finding the closest hit var closestHit = validRaycastHits[0]; // Check if the closest hit is a CardDeckDisplayer var cardDeck = closestHit.collider.GetComponent<CardDeckDisplayer>(); if (cardDeck) { InteractCardDeck(closestHit.collider); return; } // Handle interaction with a generic closest object Interact(closestHit.collider); } - User
怎么样加一些日志看看,validRaycastHits 是不是按照距离排序了呢
- Assistant
Adding logging to track the order of
validRaycastHitscan be done by usingDebug.Login Unity. This will give you insight into how the hits are sorted by distance. You can iterate over the list after sorting and log the distances to check the order. Here’s how you can add such logging: - User
- Assistant