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!

3324-unityq.png

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.

One solution I’m about to implement:

  1. Ensure that a ‘backing board’ object exists in the far distances / game world border wall which will be picked up in the RaycastAll
  2. Do a forward RaycastAll to get all entry points.
  3. Sort all the entrypoints by their distance value (order is NOT guaranteed by RaycastAll)
  4. To get the exit points of the objects:
  5. 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.
  6. This will provide you with the furthest exit point when it hits the ‘back wall’ of the second furthest… etc…
  7. Store these exit points in an exit point array while iterating through all entry points this way.
  8. Remove the furthest hit point from the entry point array as this is the back wall object.

May the source be with you,
Isaac :slight_smile:

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