- Home /
RaycastAll - get entry and exit points?
Hi, I'm working on my little project, when I stumbled with something I need help with.
I'm able to use the RaycastAll function to gather information about each collider it detects, but is there any ways that I can get the hit.point of every entry and exit point (indicated by red dots )? Please help in Javascript, thanks!
![http://img59.imageshack.us/img59/5660/unityq.png][1] [1]: /storage/temp/3324-unityq.png
Answer by IMD · Sep 12, 2014 at 11:00 AM
One solution I'm about to implement:
Ensure that a 'backing board' object exists in the far distances / game world border wall which will be picked up in the RaycastAll
Do a forward RaycastAll to get all entry points.
Sort all the entrypoints by their distance value (order is NOT guaranteed by RaycastAll)
To get the exit points of the objects:
Traverse through the entry point array backwards from array.Length - 1 to 0, shooting a simple Raycast from the furthest entry hit point to the second furthest away.
This will provide you with the furthest exit point when it hits the 'back wall' of the second furthest.. etc..
Store these exit points in an exit point array while iterating through all entry points this way.
Remove the furthest hit point from the entry point array as this is the back wall object.
May the source be with you, Isaac :)
Answer by ScroodgeM · Sep 07, 2012 at 12:51 PM
raycast and physic engine works only with forward faces of colliders, not with backwards. so one way to get exit points is to hit raycast in opposite direction.
Answer by Fabio Fernandes · Mar 18, 2015 at 06:51 PM
I would like to suggest you a solution I did develop for me.....
Consider the player like a "Hunter", and the caught object like a "Hunted".
You can achieve this by writing a code to the "Haunted" object to start verifying if it is still captured, once it is captured for the first time, like this:
Consider the "Hunter Component" code:
public class HunterComponent : MonoBehaviour {
public RaycastHit hit;
void Update () {
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
if (Physics.Raycast(ray, out hit, 20))
{
HuntedComponent component = hit.collider.gameObject.GetComponent<HuntedComponent>();
if( component != null )
{
component.OnCapture(this);
}
}
}
public bool IsInRay(GameObject outer)
{
bool stillIsInRay = false;
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
RaycastHit[] hits = Physics.RaycastAll(ray, 20);
foreach (RaycastHit outerSearch in hits)
{
if (hit.collider.gameObject == outer)
{
stillIsInRay = true;
break;
}
}
return stillIsInRay;
}
}
and the "Hunted Component" code:
public class HuntedComponent : MonoBehaviour { HunterComponent rayCasterSource; bool selected = false;
void FixedUpdate () {
if (rayCasterSource != null)
{
if(!rayCasterSource.IsInRay(gameObject))
{
OnRelease();
}
}
}
public void OnCapture(HunterComponent current)
{
renderer.material.color = Color.red;
rayCasterSource = current;
}
public void OnRelease()
{
rayCasterSource = null;
renderer.material.color = Color.white;
}
}
I hope this solution helps you guys
Bests regards
Dude, you don't even know what are you talking about, thinking about adding a component to EVERY object the man has on his scene to make it work? ... please. This is a very bad response, please dont use this, bad performance and horrible scalability.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080738im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move to closest point in front of player with raycast 0 Answers
Raycast object as far as possible? 1 Answer
Move object to raycast point. 3 Answers
Trouble with raycasting 0 Answers
How to get info of object within certain range?(Javascript) 1 Answer