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:
int layerMask = 1 << LayerMask.NameToLayer("Selection");
RaycastHit raycastHit;
Ray ray = new Ray(dataPoint, direction);
if (Physics.Raycast(ray, out raycastHit, Mathf.Infinity))
{
endPos = raycastHit.point;
}
Debug.DrawLine(dataPoint, endPos, Color.green, 1);
return raycastHit;
I get this picture:
but when I add
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??
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
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.
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!!!