Detect all collision points with Raycast

I want to cast a ray and collect all the points where it collided with objects.
But for some reason when I cast the ray, I get only 1 hit point per gameobject. The point where ray exits the gameobject is ignored. Why?

 RaycastHit[] hits;
    hits = Physics.RaycastAll(ray, MaxLength);
    for(int i = 0; i < hits.Length; i++)
        RaycastHit hit = hits*;*

GameObject goHitall = hit.transform.gameObject;
Debug.Log(“hit go (all):” + + " depth:" + hit.distance);
hit go (all):Cube depth:7.706799
hit go (all):Cylinder depth:10.10429
Here I would expect to get 2 points for each gameobject.
What can I do about this? Thank you

What you could try is calling an additional RaycastAll in the opposite direction, and reversing the resulting array. This will give you 2 arrays with indexes corresponding to the same object.


Disclaimer: I’ve offset the rays by 0.25 and -0.25 on the Y-axis so it’s easier to visualize. Please note that even those the console output shows incorrect/rounded figures of 0.3 and -0.3, this is [just the behavior of the Unity Editor][1].

  • The green ray move left to right, and is considered the forward “enter” direction.
  • The red ray moves right to left, and is considered the reverse “exit” direction.
  • Edit: I have added small arrows and ray hit points using Photoshop for clarity.


using UnityEngine;
using System.Collections;

public class RaycastExample : MonoBehaviour
	bool logged;

    void Update()
        // Raycast Origins
        Vector3 enterRayOrigin = new Vector3(-6, 0.25f, 0);
        Vector3 exitRayOrigin = new Vector3(6, -0.25f, 0);

        // Show rays in scene view
        Debug.DrawRay(enterRayOrigin, Vector3.right * 12f,;
        Debug.DrawRay(exitRayOrigin, Vector3.left * 12f,;

        // Actually cast rays
        RaycastHit[] enterHits = Physics.RaycastAll(enterRayOrigin, Vector3.right, 12f);
        RaycastHit[] exitHits = Physics.RaycastAll(exitRayOrigin, Vector3.left, 12f);

        // Reverse the second raycast so indexes between the
        // two will correspond to the same object.

        // For demonstration, log out the hits one time.
        if (!logged)
            for (int i = 0; i < enterHits.Length; i++)
                RaycastHit enter = enterHits*;*

RaycastHit exit = exitHits*;*

string enterName =;
string exitName =;

Debug.Log("Hit entering: " + enterName + " at: " + enter.point);
Debug.Log("Hit exiting: " + exitName + " at: " + exit.point);
logged = true;