Search Unity

Raycast changes direction when LayerMask is added???

Discussion in 'Scripting' started by kliment5, Jun 25, 2017.

  1. kliment5

    kliment5

    Joined:
    Dec 27, 2016
    Posts:
    9
    Hi guys,
    I am writing a script where I have raycasts from one place to another. Since the game objects that do the raycasts, already are in another gameObject with box collider, they collide with the box collider around them. Therefore, I added the LayerMask in the script but then the Raycast changes the direction.

    When I have this code:
    Code (CSharp):
    1.  
    2.         int layerMask = 1 << LayerMask.NameToLayer("Selection");
    3.         RaycastHit raycastHit;
    4.         Ray ray = new Ray(dataPoint, direction);
    5.        
    6.         if (Physics.Raycast(ray, out raycastHit, Mathf.Infinity))
    7.         {
    8.             endPos = raycastHit.point;
    9.         }
    10.         Debug.DrawLine(dataPoint, endPos, Color.green, 1);
    11.  
    12.         return raycastHit;
    I get this picture:



    but when I add

    Code (CSharp):
    1. if (Physics.Raycast(ray, out raycastHit, Mathf.Infinity, layerMask))
    then the Raycasts change their direction completely, and it looks like this:



    You can see the gameobjects from where the Raycast starts.

    I really dont understand how come that can be changed??

    Thank you in advance!!
     
  2. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    What happens if you move your debug drawline inside your if? It seems misleading how you've done it because when the if condition isn't hit that means your endpos isn't set and therefore is whatever it was before.. so if it is never hit, all lines will go to the same endpoint, even (which is what it looks like here). In other words your drawline isn't necessarily accurate to the raycast (quite inaccurate, I'd think)

    My guess (and just a guess) is your layer mask is resulting in a lot of (or all) rays not hitting.

    You could also use debug drawray and just draw the same ray.

    Bottom line is your ray won't change direction because of a layermask. You aren't drawing your line properly
     
  3. kliment5

    kliment5

    Joined:
    Dec 27, 2016
    Posts:
    9
    Hey, thanks for the reply!!
    I decided not to think about that now.. I am facing another problem.
    I have the plane with its position, and I am trying to shoot the raycasts towards it but for some reason they go on other side.

    This is the code I have:
    Code (CSharp):
    1.  RaycastHit shootRaycast(Vector3 dataPoint, Vector3 plane)
    2.     {      
    3.         RaycastHit ray;
    4.        
    5.         if (Physics.Raycast(dataPoint, plane, out ray, Mathf.Infinity))
    6.         {          
    7.               Debug.DrawRay(dataPoint, ray.point, Color.green, 1);
    8.         }
    9.  
    10.         return ray;
    11.     }
    For some reason, the direction value is not the same as I gave it.. I dont understand why?

    Shouldnt Plane position and ray.point be the same? Since I am firing the raycast in that direction??
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    RaycastHit.point:
    https://docs.unity3d.com/ScriptReference/RaycastHit-point.html

    It's a position... not a direction.

    You're using 'plane' as the direction, so I assume it's the plane's surface normal.

    ray.point - dataPoint should be a vector parallel to plane. (ray.point - dataPoint).normalized should == plane.normalized.

    But plane and ray.point should NOT be the same.
     
  5. kliment5

    kliment5

    Joined:
    Dec 27, 2016
    Posts:
    9
    Hey, thanks for the reply.
    I am getting really confused now.. What I want to do actually is, since I have these points in 3D space, I need them to shoot raycasts towards the plane as they would be pixeled on the surface of the plane and I can see the 2D image of the 3D representation depending on which location the plane is.
    Any idea how can I fire the raycasts towards the plane?
    Thank you!!!!