- Home /
There is a "Ignore Raycast" layer, but how would you make a "Raycast Only" layer?
I believe the title is self-explanatory, but I'll add some details. There is a plane that serves as a navmesh for the spaceships, and I need that nothing is able to collide with it except the raycast from the mouse.
Just use a layer mask in your raycast. Put your navmesh plane on a new layer you create.
if (Physics.Raycast(transform.position, transform.forward, out hit, $$anonymous$$athf.Infinity, myLayer$$anonymous$$ask)){
//do stuff
}
Answer by NPnpNPnpNP · Jan 27, 2021 at 11:19 PM
You can create a custom layer, and disable all collisions of this layer in the project settings.
Simply go to Edit->Project Settings->Physics(or Physics 2D), here you can disable collision with other layers. I'm not sure if this solution works, so let me know if its works!
Answer by Bunny83 · Jan 28, 2021 at 05:52 AM
Note if you need an infinite plane to raycast against just to determine the intersection point of a ray with that plane, you should simply use Unity's Plane struct. It's a pure infinite mathematical plane. It's way more performant to do a raycast against that plane because it's just simple math.
All you have to do is initialize a Plane struct with a point on that plane and a normal vector. Usually you just want Vector3.up if the plane should be an x-z-plane or Vector3.forward if you want a x-y-plane. You can use an empty gameobject to specify the point on the plane if you want it to be located relative to a certain point. If the plane should always go through the world origin, of course you could also hardcode that point.
Such a Plane is nothing more heavy than a Vector4. It's literally just 4 floating point numbers, nothing more. It represents a mathematical plane in normal form. The usage is quite simple. Just initialize your Plane with the normal vector and a point on the plane like that:
Plane plane = new Plane(normal, point):
where "normal" and "point" are Vector3 values. Now you can simply create a Ray from the mouse coordinates and use the Raycast method of the Plane struct. It gives you the distance of the intersection along the Ray. You simply use the GetPoint method of the Ray to get the world space point of the intersection.
Plane plane = new Plane(Vector3.up, new Vector3(0, 0, 0)):
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(plane.Raycast(ray, out float dist))
{
Vector3 hitPoint = ray.GetPoint(dist);
// use hitPoint here
}
The great thing about the Plane is, that it is infinite since it directly calculates the intersection between the mathematical description of a plane and a ray. Of course nothing can "block" the ray and it doesn't interfer with anything else.
Your answer
Follow this Question
Related Questions
Project branching into two separate projects that share no dependencies 1 Answer
How to get all layers included in a LayerMask? 4 Answers
Survival Shooter Tutorial .. Floor Layer Missing ?! 0 Answers
Strange 'Glitch' while trying to apply the built-in layer "Ignore Raycast" via code 0 Answers
Only interact with objects if they're visible and unobstructed by other objects? 0 Answers