Collider.Raycast

I’m having trouble using Collider.Raycast.

I’ve tried it a few different ways, but I keep getting the error: “MissingMethodException: Method not found: ‘UnityEngine.MeshCollider.Raycast’.” As a test, I copied the example script from the docs directly and tried it on a simple GameObject with a BoxCollider.

function Update () {
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
    if (collider.Raycast (ray, hit, 100)) {
    Debug.DrawLine (ray.origin, hit.point);
    }
}

It gave me the same MissingMethod error.

Using Physics.Raycast works fine, but I’d really like to use Collider.Raycast if possible. I only want to trace against the Collider that is casting the ray, and I’d love to skip having to build a layerMask.

Thanks,
-Dan

try this

var coll : Collider;

function Update () { 
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
    var hit : RaycastHit; 
    if (coll.Raycast(ray, hit, 100)) { 
    Debug.DrawLine (ray.origin, hit.point); 
    } 
}

I think its because it was trying to cast the ray on the collider of the object the script is on

Thanks, Beast.

That works, but it strikes me as odd that I have to point the collider back to itself using a public variable.

The way I’m reading the docs I would expect to be able to access the collider component directly using the built-in variable collider, or at worst do something like thisCollider = GetComponent(Collider), and then thisCollider.Raycast(…). In the example code they call collider.Raycast directly, but it doesn’t seem to work in the current version of Unity.