- Home /
How does collider.Raycast really work?
First thing first! Here's my code!
#pragma strict
private var distance : float = 5.0;
function Update () {
var ray = new Ray(transform.position, -Vector3.right);
var hit : RaycastHit;
if(collider.Raycast(ray, hit, distance)){
Debug.Log(hit.transform.name);
}
Debug.DrawLine(ray.origin, ray.origin + ray.direction * distance);
}
Seems pretty much 90% the same as it is in the help file.
But the raycast doesn't detect the other collider! (trust me, it has a collider, and it is intersecting!) 125125
Can anyone tell what I'm doing wrong?
Answer by carlos.castaneda · Jan 12, 2013 at 05:04 AM
@aldonaletto, collider.Raycast is execute when this collider receive a ray, it means the origin of the ray is from other object.
You can use that to highlight object when select with the mouse to give you a example.
This is very helpful. I finally understand how the collider.Raycast really works after I saw your answer here.
Answer by fantazm · Aug 26, 2011 at 02:54 PM
Is your collider still set to trigger? If so, uncheck it.
Answer by dogzerx2 · Aug 26, 2011 at 03:00 PM
Actually I solved this by using Physics.Raycast instead of collider.Raycast. Collider trigger is unchecked.
Funny thing: the docs say "collider.Raycast ignores all colliders except this one". I used to think it was a typo, but based on your description it seems the docs are serious about this: this raycast really ignores all colliders! Since I just can't think about a reason to cast a ray to myself, this function seem to be totally useless.
@aldonaletto it's often useful. Say you want to check how high above the ground you are? You cuold either use Physics.RaycastAll and iterate through them, find the terrain and check, or you grab the terrains collider and use collider.Raycast.
Your answer
Follow this Question
Related Questions
How can I change this script? 0 Answers
Starting animation with collision 0 Answers
Touch collision detection with a sprite 0 Answers