Unity question (Javascript)

12 posts

Flag Post

Hello, I have a question about fog in Unity (free version).

I have a script (in which I will soon post) which changes the fog
in the scene when the player enters and changes it back when he/she
exits an object tagged with the appropriate tag.

So if Player enters the “trigger-zone” of Object A, the fog changes.
When Player exits, the fog is changed back.

However, the idea is to make a sort of underwater-feel, with different fog
levels for different “depths”.

The problem is that when I have one script on the Player
which changes the fog, it works fine. When I add another one (to
simulate deeper levels (thicker, darker fog)), the first one is isn’t working
and only the darker fog-level works.

I’ve tried (just to see what would happen) to add more scripts with
even darker fog, and it’s only the last script that works.

Here’s the script:



#pragma strict

var fog = false;
var fogColor = Color (0, 0, 1, 1);
var fogDensity = 0.04;
var skybox : Material;

function OnTriggerEnter(other : Collider) {
	if(other.tag =="water")
		fog = true;
		RenderSettings.fog = fog;
		RenderSettings.fogColor = fogColor;
		RenderSettings.fogDensity = fogDensity;
		RenderSettings.skybox = skybox;
	}
	
function OnTriggerExit(other : Collider) {
	if(other.tag =="water")
		fog = false;
		RenderSettings.fog = fog;
	}


Let’s say two copies of this script is used. The first one tagged
“water” and the fog density is 0.04
The next one will be tagged “deeperwater” and have a fog density of
0.1 or something.

It would be wonderful if anyone knew, and told me, what is wrong.
I will share more information if necessary, in case I have forgotten
something (in which I probably have).

Thanks in advance, Predator

(Sorry for the grammatical errors. I’m a little rusty :P)

 
Flag Post

If my sentence-syntax is confusing, please tell me.
I’d be happy to clarify.

 
Flag Post

Hi mate,
I’m no expert in unity, but if you put your code into the level class, or even make a class for the fog… you could use a switch block (case and break) to check which collider your player has triggered and set the fog density accordingly…
Hope that helps

 
Flag Post

Well, I’ve made a minor breakthrough.
Before, the fog only changed when the player entered
the collider which was the last one on the list.

Now, the fog changes on both, but it’s the same fog.

Let me clarify:
Again, the idea is that when the player enter cubeA, the fog should
appear and have a light blue colour, and when he enters the second
one, a darker blue colour.

I’ve gotten so far as for the fog to appear in both cubes,
but it’s the darker fog which appears in both.


@PlatterSpud

I thought maybe the

if(other.tag ==“water”)


was sufficient to check which it was colliding with,
but I will check out your method.

A million thanks, friend!

 
Flag Post

Hi mate,
like I said, I’m no expert in Unity (and it’s been a while since I practiced JS) but in any code you chose to build your game, it’s not efficient to duplicate your code wherever you want to use it… it’s far better to think of every eventuality of use in one instance and cater for it there… So where you chose to put your code is up to you, but only put it in one place and have it called when needed… so for your code, an example that might work could be…


#pragma strict

var fog = false;
var fogColor = Color (0, 0, 1, 1);
var fogDensity = 0.01;    // just set it at a default value for now
var skybox : Material;

function OnTriggerEnter(other : Collider) {
switch{
   case 1(other.tag ==“water”): fogDensity = 0.04;
break;
   case 2(other.tag ==“deepwater”): fogDensity = 0.06;
break;
   case 3(other.tag ==“verywater”): fogDensity = 0.08;
break;
   case 4(other.tag ==“extremewater”): fogDensity = 0.1;
break;
}
fog = true;
RenderSettings.fog = fog;
RenderSettings.fogColor = fogColor;
RenderSettings.fogDensity = fogDensity;
RenderSettings.skybox = skybox;
}

or something like that… I haven’t used JS in so long I cant remember if my syntax is correct… I’m sure you get the idea anyway.
Glad to help.

 
Flag Post

Wow, I haven’t even considered the “switch” option.

Thanks, friend. I owe you a big one.

 
Flag Post

Whelp, got an error.

Apparently, the compiler wants a “(” in the line with the “switch”.

expecting (, found ‘{’

Here:

function OnTriggerEnter(other : Collider) {
switch {

The compiler wants a parenthesis rather than a squigglybracket.


I’m sorry if I appear to be throwing the troubles on you.
I am trying to find out on my own, I’m just leaving it here if
you know what’s wrong.

Thanks

 
Flag Post

Ah, sorry, it probably requires a value to the switch statement…

umm try something like…


function OnTriggerEnter(other : Collider) {

var value = other.tag;

switch(value){
   case 1(value ==“water”): fogDensity = 0.04;
break;
//etc....

it’s just a thought… try it

 
Flag Post

Alright, now I get


It is not possible to invoke an expression of type ’int

 
Flag Post

OK, let’s go back to basics with this, since I’m out of practice and cant see the rest of your code… does it work if you do it as a series of “if” statements?


#pragma strict

var fog = false;
var fogColor = Color (0, 0, 1, 1);
var fogDensity = 0.01;    // just set it at a default value for now
var skybox : Material;

function OnTriggerEnter(other : Collider) {

   if(other.tag ==“water”){ fogDensity = 0.04;
}
   else if(other.tag ==“deepwater”){ fogDensity = 0.06;
}
  else if(other.tag ==“verywater”){ fogDensity = 0.08;
}
   else if(other.tag ==“extremewater”){ fogDensity = 0.1;
}
fog = true;
RenderSettings.fog = fog;
RenderSettings.fogColor = fogColor;
RenderSettings.fogDensity = fogDensity;
RenderSettings.skybox = skybox;
}

it’s not the most efficient way, but at least (if it get’s it working..) you can build on it…

 
Flag Post

I found another, even more tedious way to do it,
based on your idea.


function OnTriggerEnter(other : Collider) {

			if(other.tag =="water") {
				fog = true;
				RenderSettings.fog = fog;
				RenderSettings.fogColor = Color (0, 0.4, 1, 0.5);
				RenderSettings.fogDensity = 0.04;
				RenderSettings.skybox = skybox;			
				}
			
			
					else {
						fog = false;
						}
		
		
		
		
		 	if(other.tag =="alittledeep") {
				fog = true;
				RenderSettings.fog = fog;
				RenderSettings.fogColor = Color (0, 0.1, 0.5, 0.8);
				RenderSettings.fogDensity = 0.09;
				RenderSettings.skybox = skybox;
				}
			
			
					else {
						fog = false;
						}
	
	
	
	
			if(other.tag =="deeperwaters") {
				fog = true;
				RenderSettings.fog = fog;
				RenderSettings.fogColor = Color (0, 0, 0.3, 1);
				RenderSettings.fogDensity = 0.4;
				RenderSettings.skybox = skybox;
					}
		
		
					else {
						fog = false;
						}
			
			
			
			if(other.tag =="challengerdeep") {
				fog = true;
				RenderSettings.fog = fog;
				RenderSettings.fogColor = Color (0, 0, 0.5, 1.9);
				RenderSettings.fogDensity = 0.8;
				RenderSettings.skybox = skybox;
				}
				
				
					
					else {
						fog = false;
						}
			
			
			
	}
			
function OnTriggerExit(other : Collider) {
		
		if(other.tag =="water") {
			RenderSettings.fog = fog;
			}
			
		if(other.tag =="alittledeep") {
			RenderSettings.fog = fog;
			}
			
		if(other.tag =="deeperwaters") {
			RenderSettings.fog = fog;
			}
		
		if(other.tag =="challengerdeep") {
			RenderSettings.fog = fog;
			}

}

It works, though.

Thanks for all your help, PlatterSpud. I really appreciate it.

 
Flag Post

The correct syntax of switch is


switch(other.tag) {
case "water": fogDensity = 0.04;
break;
case "deepwater": fogDensity = 0.06;
break;
case "verywater": fogDensity = 0.08;
break;
case "extremewater": fogDensity = 0.1;
break;
}