- Home /
I am having trouble successfully raycasting with my AI bot.
I have an AI bot that I am trying to utilize raycasting with. For some reason, I cannot get it to work. Here is the code I am using:
var hit : RaycastHit;
var layerMask = 1 << 8;
Physics.Raycast(transform.position, transform.TransformDirection (Vector3.forward), hit, 100, layerMask);
Any help with this would be appreciated.
Thanks.
Answer by Jaap Kreijkamp · Jan 14, 2010 at 03:37 AM
You could be a bit more specific! What the code seems to lack is, it doesn't actually check if Physics.Raycast did hit something. Your code would look something like this (transform.forward
is just a shorthand for you transform.TransformDirection
thing):
var hit : RaycastHit;
var layerMask = 1 << 8;
if (Physics.Raycast(transform.position, transform.forward, hit, 100, layerMask)) {
// now we have a hit and hit var contains the info about what we hit.
}
also, if the origin (point of centre) of your bot is on the floor it would be useful to move the position of the ray a bit up (make it transform.position + transform.up
for example).
The layerMask you defined means you're only looking at objects in Layer 8, that's what you want I guess? If you want to test for objects in all layers just remove the ", layerMask
" part in the code.
Answer by Ashkan_gc · Jan 14, 2010 at 01:53 PM
use the debug.DrawRay to see if your ray really goes where you want and check if the collider of your other object has the correct size or not. also check if it's in layer 8 or not. it should not be in "ignore raycast" layer
Answer by Garrett Chase · Jan 14, 2010 at 11:56 PM
Thanks so much. Yeah, I wasn't checking whether or not it hit something. Also, the problem was when my team modeled the bot, the axis got messed up so I needed to be casting the ray "down" instead of "forward" in order to actually cast it in the direction the bot was facing. Thanks :)
Your answer
Follow this Question
Related Questions
Problems with raycast obstacle avoidance 1 Answer
AI Avoiding Obstacles Problem 1 Answer
AI create path, help. 1 Answer
How can I find if a raycast has passed through two points? 2 Answers
Why Do Raycasts Ignore NavMesh Agents 4 Answers