x


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.

more ▼

asked Jan 14 '10 at 01:11 AM

Garrett Chase gravatar image

Garrett Chase
25 2 2 3

(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest

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.

more ▼

answered Jan 14 '10 at 03:37 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jan 14 '10 at 01:53 PM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

(comments are locked)
10|3000 characters needed characters left

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 :)

more ▼

answered Jan 14 '10 at 11:56 PM

Garrett Chase gravatar image

Garrett Chase
25 2 2 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1531
x960

asked: Jan 14 '10 at 01:11 AM

Seen: 1778 times

Last Updated: Jan 14 '10 at 01:11 AM