Search Unity

raycasting help, selective raycasting

Discussion in 'Scripting' started by eteeski, Mar 5, 2010.

  1. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    just need to know, whats the easiest way to raycast, but ignoring all objects except for ones with a certain tag?

    raycast returns the collider that is first hit, and raycastall returns an array of all the colliders hit?

    it sounds like layers would help out for this, but i've already got my game set up so that i would be easier to use tags because the only objects i want the raycast to look for are the ones with that tag.
     
  2. Discord

    Discord

    Joined:
    Mar 19, 2009
    Posts:
    1,008
    After you use the raycast, just check the tag of whatever it hit. If the tag is one that you want to ignore, simply use a return statement to prevent the rest of the code from being executed.
     
  3. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    right, but the trouble is that i need that raycast to go through the objects that i dont want it to hit. right now, it just stops at the first collider it hits, returns tag == false, and ends there. i want it to travel the entire length of the raycast before it stops looking.

    one way i was thinking was to take RaycastAll and then putting the result into a for loop, and just cycle through each spot on the arrays returned from the RaycastAll and return true to the script if any of the collisions have a certain tag. but i cant get the code for this working right.
     
  4. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    No. Just put all the objects that you want to ignore on the 'Ignore Raycast' layer.

    Simples.
     
  5. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    maybe you could look at my code and help me figure out why it's not working

    Code (csharp):
    1.  
    2.     //waiting for update lets the game only have to do this a number of times a second less than the frame rate
    3.     waitingForUpdate -= Time.deltaTime * updatesPerSec;
    4.     if (waitingForUpdate <= 0)
    5.     {
    6.         //going through my grid by x and y
    7.         var thex = 0;
    8.         var they = 0;
    9.         var stop = false;
    10.         while (stop == false)
    11.             {
    12.             //move to the position where i want this grid to be
    13.             transform.position = tracker.position;
    14.             transform.rotation = tracker.rotation;
    15.             //maxx and maxy is the size of the grid, gridSize is the number of rows and collums the grid is devided by
    16.             transform.Translate(maxx/2 * -1, maxy/2 * 1, 0);
    17.             transform.Translate((thex * squareWidth) + squareWidth/2, -1 * (they * squareHeight) + squareHeight/2, 0);
    18.             //this is where im having trouble
    19.             var hit : RaycastHit[];
    20.             hit = Physics.RaycastAll(transform.position, transform.forward, distDetect, 8);
    21.                 if (hit.length)
    22.                 if (hit[0].collider.tag == "LevelElements"){
    23.                     Instantiate(particle, hit[0].point, transform.rotation);//i make particle where there is a collision
    24.                     gridBlock[(they * gridSize) + thex] = true;}               //so i can see where the collisions are
    25.                 else                                                                     //and i can see if it's working right
    26.                     gridBlock[(they * gridSize) + thex] = false;
    27.             //here i increase the x and the y accordingly
    28.             thex ++;
    29.             if (thex > gridSize){
    30.                 they ++;
    31.                 thex = 0;}
    32.             if (they > gridSize)
    33.                 stop = true;
    34.             }
    35.         //here i make an array that lists all the spaces in the grid that didn't have a collision
    36.         var track = 0;
    37.         while (track <= gridSize * gridSize)
    38.         {
    39.         if (gridBlock[track] == false) //im getting an error here "index is more, less, or equal to the list count" ?
    40.             freeBlocks.Push(track);
    41.         track ++;
    42.         }
    43.         waitingForUpdate = 1;
    44.     }
    45.  
    the forum's width kind of mixes it up and makes it hard to read, might be clear is you quick copy and paste it into unitron.
     
  6. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    Hi, I can't see anything obviously wrong with your code, but I only took a quick look.

    However, my suggestion was nothing to do with code - I suggested that you put all the objects that you want to ignore on the 'Ignore Raycast' layer (in the inspector)... see image attached.

    So, the question to you is - did that work?
     

    Attached Files:

  7. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    no, i did put all the other objects into a layer (layer 8), but my script doesn't seem to be detecting any collisions, inside layer 8 or not. so im still doing something wrong...
     
  8. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    Hi, that is different to what you posted earlier:

    ...are you now saying it does not detect a hit at all?

    It's very difficult to see where the problem is just from looking at your coe and not the project, for all we know the raycast could be shooting out in the wrong direction?

    If I were you, I would break the problem down into smaller chunks, starting with just getting a raycast to detect a hit.

    Then check that you can selectively ignore certain objects using the 'ignore raycast' layer - THEN build up to your more complex code here...

    PS. An observation: are you sure you need to be raycasting at all? It appears to me that you are making some kind of a board game with a gridded layout? If so, you could probably check what is sitting at each position using a more flexible technique - eg. via an Array? - just a thought?
     
  9. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    no, i had all of this code working earlier by using raycastall. the grid system works fine and the rays are going in the right direction :b. what i cant get working is the raycasting with layers. it seems like it would be simple to do. like i said earlier, i just need to make a raycast that ignores a layer.

    sorry, i was working on that grid problem in another thread and i guess i got them mixed up. but anyways, the raycasting and layers is what im asking about here.