- Home /
collider.Raycast problem
I have this script that should turn an object when it approaches to another:
function Update () {
var mainRay = new Ray(transform.position, transform.TransformDirection(Vector3.forward));
var mainHit : RaycastHit;
var sight = 10.0;
if (collider.Raycast(mainRay,mainHit,sight)) Turn();
else transform.Translate(Vector3.forward*speed*Time.deltaTime);
}
For some reason it doesn't work and the Turn() function is never called. I don't want to use Physics.Raycast, because I don't want my character hitting corners or such things. What am I doing wrong?
Answer by kolban · May 20, 2012 at 04:50 PM
The Collider.Raycast() method casts a ray that will be true only if the ray hits THIS collider. The Physics.Raycast() method will cast a ray that will be true if it hits ANY collider. In your code fragment, I am assuming that you wish to cast a ray from the current game object outwards in some direction. As such, Physics.Raycast() is what you want. Using Collider.Raycast() will never be true as (if I am understanding correctly) you are always casting out from the game object trying to hit the collider which is yourself.
Considerating the performance and your explanation, so is the Collider.Raycast better to use than the Physics.Raycast or are they equal?
Thanks for the help. I misunderstood the script reference...
@$$anonymous$$aze_Senshi - There is a fundamental difference between the Physics.Raycast and the Collider.Raycast. One would use Physics.Raycast to cast a ray to see if ANY collider will be struck in the direction of the casting. One would use Collider.Raycast to detect if a SPECIFIC collider (the one on which the Raycast is called) will be struck by the Raycast. With this in $$anonymous$$d, the two Raycast methods being discussed will never be "equal" as they do very different things. Obviously, if ALL you need to do is to detect if a casting hits a specific collider and no other collider, then the Collider.Raycast is likely to be more efficient.
Answer by Kaze_Senshi · May 20, 2012 at 02:16 PM
Hmmm the code compiles OK with that "Turn()" without ';' at the end? Here I use Physics.raycast instead of the collider one and it works well. I don't know well the difference between they, but you can try this one.
if( Physics.Raycast( origin, direction, raycastHitObject, rayDistance ) )
{ /* ray collision detected */ }
No, the compilation runs fine, the missing ';' was simply a typo. Physics.raycast doesn't do what I want. As far as I know it sends a ray that detects only the center of the objects, collider.raycast should detect also the points of the collider, but doesn't seem to work to me...
Your answer
Follow this Question
Related Questions
Issues with Navmesh Pathfinding and casting a Ray on a transform position 0 Answers
Raycast 2D hits sprite with collider, but returns error when not hitting any collider 2 Answers
Raycast, Linecast, sphere collider or capsule collider? 0 Answers
using multiple colliders for a gameObject and it's children 1 Answer
raycasthit questions 2 Answers