- Home /
Diagonal Raycasting to detect platform returns true, even when false
So I want to make a dynamic AI enemy using ray casting in a two dimensional platformer game. The objective is to have it detect whether it is at the edge of a platform so that it can turn around. If it worked I could just plop down the enemy anywhere on a platform and would not have to set up a bunch of triggers which can be slow. The only thing is that the physics.raycast is returning falsely. When there is no platform it still returns true, as if it is hitting the platform. I even made a debug.drawray to check that the numbers were right. Here is the code.
function DropCheck () {
drop.start = transform.position + drop.offset;
var forwards = transform.TransformDirection(drop.angleForward);
var back = transform.TransformDirection(drop.angleBackward);
Debug.DrawRay (drop.start,forwards*drop.lenght,Color.blue);
if(!Physics.Raycast(drop.start,forwards,drop.lenght)){
Debug.Log("No ground");
}
Debug.DrawRay (drop.start,back*drop.lenght,Color.blue);
if(Physics.Raycast(drop.start,back,drop.lenght)){
Debug.Log("Ground");
}
else{
Debug.Log("no ground");
}
}
The drop.angleForward is a Vector3(1, -1, 0), and drop.angleBackward is a Vector3(-1,-1,0). For my tests a length of 2 intersects the platform and returns as true, which is what I want. The drop.offset was to test in playmode whether changing the origin would work.
Perhaps I just not understanding how ray casting really works. I'm imagining it as a single line through the 3D space, though here it is acting, well not quite sure, more like a plane?
Hey there, I didn't really get time to read your code, but you are misspelling .length every time! Try editing it and see if this fixes your problem.
haha, yup your right. lucky, or perhaps unlucky, once I had defined that class variable I just used the IDE's auto fill. So it is consistent and doesn't effect this script.
Answer by jspease · Mar 01, 2012 at 06:43 AM
Maybe your raycast is hitting some other collider besides the platform, such as a collider on your AI object itself. Usually you need to specify a layerMask in the last (optional) argument to Physics.Raycast to avoid that sort of problem. For example, you could set the layer on your object to something different from the platform, and then do something like:
var layerMask = ~(Physics.kIgnoreRaycastLayer | (1 << gameObject.layer));
if(Physics.Raycast(drop.start, back, drop.length, layerMask)) {
Well I did think that might be the case. I was going to eventualy try layers, but the thing is if I put it on, lets say,the player character which can move and jump. If I jump then the raycast returns a false value, i.e. not hitting anything. However if I move right to the edge of a platform, and I can see that the debug.drawray is over the edge, it still returns true. I even put in the off set and no matter how far horizontally I move the ray it still says it hits, though if I move the ray up enough, or down, it returns false, go figure. I even tried one ray at a time and at both ends of the platform.
Then, I would use a version of Physics.Raycast that takes "out hitInfo : RaycastHit". The RaycastHit gives you all sorts of useful information, for example it tells you the exact point it thinks the ray hit (so you can draw a line to it with Debug.DrawLine), and it tells you which transform it hit so you can easily print out the name of which gameObject the ray hit. I don't see anything really wrong with what you posted, but maybe there's a problem with how you set up the collision or game objects in your scene.
By the way, maybe you should normalize your "forwards" and "backwards" vectors, since TransformDirection will not automatically normalize them. I think it shouldn't matter for Physics.Raycast, but it's nice to have the correct direction vector for your other calculations and debug drawing.