Search Unity

Serie of raycast Parallel to a plane

Discussion in 'Scripting' started by tet2brick, Jun 6, 2012.

  1. tet2brick

    tet2brick

    Joined:
    Jun 24, 2009
    Posts:
    77
    Hi all :)

    I want to make a serie of raycast every 1 unit on a plane.

    Code (csharp):
    1. for(var i = 0; i < width; i++) {
    2.                    
    3.     listNodes[i]=new Array(height);
    4.     for(var j = 0; j < height; j++)
    5.     {
    6.         var hit : RaycastHit;
    7.         var raycastStart=Vector3(i+startCorner.x,targetPlane.transform.position.y+100,j+startCorner.z);
    8.        
    9.         if (Physics.Raycast (raycastStart, -Vector3.up, hit, Mathf.Infinity,finalMask))
    10.         {
    11.        
    12.             var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    13.             cube.transform.position = hit.point;                           
    14.                            
    15.         }
    16.     }
    17. }
    The problem is that it's working as long as the plane is not rotated. If I rotate the plane, the raycastStart is not parallel to the plane anymore...

    I know it's more a geometry matter than coding, but I don't see how to do it right now :p

    Any idea?

    Thanks a lot :)
     
    Last edited: Jun 8, 2012
  2. foxter888

    foxter888

    Joined:
    May 3, 2010
    Posts:
    530
    first of all you can't rotate the object because the way you are doing the raycast it's just for the whole world, pretty much you need the object axis that you are trying to work on. for example:

    if i say Vector3.right this would be using the world x axis versus saying transform.right.
    so you might want to get the object first in a variable and what not.
     
  3. tet2brick

    tet2brick

    Joined:
    Jun 24, 2009
    Posts:
    77
    Thank you for your reply :)

    I've think of that, but if I use :
    Code (csharp):
    1. Physics.Raycast (raycastStart, -pathParent.up, hit, Mathf.Infinity,finalMask)
    It take the y position of the plane, so it work for the first "line" of raycast, the others raycast start from the same Y position. If search for a way to adapt the loop to the plane rotation.
     
  4. SDuke

    SDuke

    Joined:
    Jun 4, 2012
    Posts:
    61
    if you look at this you will see that all you really have to do to fix the problem is multiply your points by the rotation matrix of that plane.
     
  5. tet2brick

    tet2brick

    Joined:
    Jun 24, 2009
    Posts:
    77
    Thanks Sduke, it solved my problem! :)