Raycast wont work on plane!

Hey guys, im struggling here a bit with raycasting. I’m trying to achieve a fog of war effect so when my players run beneath the fog it gets lit up. Now i think i understand the theory behind it, but i cant even get the raycasting to work!!

At the moment i have this:

RaycastHit hit;
			Vector3 rayUp = transform.TransformDirection(Vector3.up);
			
			if (Physics.Raycast(transform.position, rayUp, out hit))
			{
				if(hit.collider.tag == "FOG")
				{
					print("hitting fog " + hit.collider);
				}
			}

this only works when my object has a box collider on it. Whenever i try to use a mesh collider it just doesn’t detect a hit from the ray at all!
I’ve been having a look at this thread. But i just cant get it to work.
I’ve tried using unity plane gameObjects as well as an imported plane from Maya.

Am i missing a simple step? Thanks!

A plane is one sided, are you casting from the correct side?

hmm, well i was assuming that the plane would be the correct way after inserting it.
tries the other way around
Ah yes! Now i can access the mesh collider itself. But if the plane is upside down, the material can only be seen from one side. Now there is essentially no “fog”…

Wrap the the fog texture on a box instead, usually that’s what I do. Or if you have use for a material with backface culling switched off, use that. Stupid planes and their one-sidedness, it has always bothered me.

Yeah, so frustrating, been sitting here for hours trying different things…
Well, this is the aproach that i might take - still have the plane facing upward, but do the raycasting from the top coming down.

So this is my current code:

int layerMask =  4 << 8;
			Debug.DrawRay (transform.position + new Vector3(0.0f, 50.0f, 0.0f), -Vector3.up * 100.0f );
			RaycastHit[] hits;
			hits = Physics.RaycastAll(transform.position + new Vector3(0.0f, 50.0f, 0.0f), -Vector3.up, 500, layerMask);
			
			for(var i=0;i<hits.Length;i++)
			{
				RaycastHit hit = hits[i];
				MeshFilter filter = (MeshFilter)hit.collider.GetComponent("MeshFilter");
				//MeshFilter filter = (MeshFilter)hit.transform.gameObject.GetComponent("MeshFilter")) ;
				Vector3 relativePoint;
				//print(hit.transform.gameObject);
				if(hit.collider.tag == "FOG")
				{
					//print("Hitting fog");
					if(filter)
					{
						relativePoint = filter.transform.InverseTransformPoint(hit.point);
						//print(relativePoint);
						FullMesh(filter.mesh, relativePoint, radius); 
					}
				}
			}

I dont think im accessing the meshfilter porperly. I tried it with

MeshFilter  filter= hit.collider.GetComponent(MeshFilter);

but that wouldn’t work.

I think there is an issue now, because i can’t access the relativePoint. It just comes up as (0.0, 0.0, 0.0)…

You should try taking a look at Plane. Not sure i entirely grasp the problem but if the fog is like never ending and just under a plane you can use

// plane facing up, paralel to zero ( origin )
Plane fogPlane = 
    new Plane(Vector3.up, Vector3.zero);
// are we above or below
bool abovePlane = 
    fogPlane.GetSide(transform.position);

you can use other methods in Plane such as Plane.Raycast but it sounds like you really care about if sometings above it or below it.

if your using a collider you could use collider.bounds.max or .min for the GetSide if you want it to check only partial or complete behind or infront of the plane as long as the plane is axis aligned

I’ve not fully read this thread, as I’ve run out of time, but have you tried “convex” on the mesh collider?

You are not accessing it properly, there’s a huge syntax error. If you want to use GetComponent, it must be typed like this:

MeshFilter filter = hit.collider.GetComponent<MeshFilter>();

That will work. I didn’t have time to read through your LARGE snippet of code, so if there are also syntax/logic errors in there, I didn’t catch them (also, it’s very oddly formatted and makes it very hard for people to read… Perhaps you can take some time to format your code snippets so that people can better read them and help you).