x


Raycasting not working

So I have these small panels (0.1 scale, not sure if I should be doing that, or just making the map bigger) I have a larger box collider on the panels, and they are a rigidbody. My code looks like this (Layer 8 is my flashlight, which I don't want the panel to care about, otherwise it should turn left before it hits the object, which it isn't doing)

void Start()
{
    ray = new Ray(transform.position, transform.forward);
}

void Update() { LayerMask layerMask = 1 << 8;

        if (Physics.Raycast(ray,out hit,50f,layerMask))
        {

           transform.Rotate(Vector3.up, -90 * 5 * Time.smoothDeltaTime);
        }
        else
        {
            if (UpDown)
                transform.Translate(0, 0, 2 * Time.deltaTime, Space.Self);
            else
                transform.Translate(0, 0, 2 * Time.deltaTime, Space.Self);

        }
}

EDIT: When I use this code, I do hit the wall (though I spin in circles as walls are all around) whenever I add a distance, the plane just walks right up to the wall.

if (Physics.Raycast(ray,out hit))
            {
                Debug.Log(hit.distance.ToString() + " " + hit.collider.gameObject.name);
               transform.Rotate(Vector3.up, -90 * 5 * Time.smoothDeltaTime);
            }
            else
            {
                if (UpDown)
                    transform.Translate(0, 0, 2 * Time.deltaTime, Space.Self);
                else
                    transform.Translate(0, 0, 2 * Time.deltaTime, Space.Self);

            }
more ▼

asked Aug 24 '11 at 04:44 PM

cookies gravatar image

cookies
173 11 13 16

right now I am using a second collider, and if wall then I am turning... seems to work, what are the cons of doing it this way?

Aug 24 '11 at 05:31 PM cookies
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

The Layers page on the Unity Documentation shows how to do this.

You did create a layer mask but that layer mask is set to hit only layer 8. You'll want to invert it before you use it.

layerMask = ~layerMask;
more ▼

answered Aug 24 '11 at 05:22 PM

Rennat gravatar image

Rennat
664 5 8 18

I'm getting "Operator '~' cannot be used with an expression of type `UnityEngine.LayerMask'" when I write exactly that---

Sep 30 '11 at 07:44 PM juanelo
(comments are locked)
10|3000 characters needed characters left

I'm not sure if you have realized that: You set your Ray in Start so it will never change it's position or direction. I can see that you move and rotate your object in the script but your ray will ALWAYS stay at the initial position. It doesn't move along with the object. I guess you want to reinit the ray in Update right before your Raycast.

Btw: The layerMask parameter of the Raycast function is just of type int. The LayerMask type has been invented to easily create a layermask value and to display it in the inspector. When you "create" your mask on your own by left-shifting (1 << 8) you don't want to use the LayerMask type at all. Just use an int.

If you want to raycast against everything but layer 8 the value you need would be ~(1<<8)

I guess you also don't want to raycast against the ignoreRaycast layer so you should setup your mask like this:

int myLayerMask = Physics.kDefaultRaycastLayers & (~(1<<8));
// or
int myLayerMask = ~((1<<2) | (1<<8));
more ▼

answered Sep 30 '11 at 09:30 PM

Bunny83 gravatar image

Bunny83
45.3k 11 49 207

I see, thanks, Bunny

Oct 01 '11 at 04:51 PM juanelo
(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:

x1530
x180

asked: Aug 24 '11 at 04:44 PM

Seen: 923 times

Last Updated: Oct 01 '11 at 04:51 PM