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.
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.
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
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).