Author Topic: Adding underwater FX to an instantiated camera?  (Read 7241 times)

derkoi

  • Newbie
  • *
  • Posts: 14
    • View Profile
Adding underwater FX to an instantiated camera?
« on: October 25, 2013, 05:36:57 AM »
Hi, I'm making a fishing game & the player can choose where to fish. They first walk around the lake in first person. If they walk in the water the underwater FX are there and work fine however when they choose their fishing spot I instantiate the rods and other game objects including an underwater camera to switch to when they put a fish back in the water after catching it, however none of the underwater FX are on this new camera. I tried setting it to a main camera but that doesn't work either.

Could you please help?

Thanks.

Chingwa

  • Administrator
  • Hero Member
  • *****
  • Posts: 1587
    • View Profile
    • Tanuki Digital
Re: Adding underwater FX to an instantiated camera?
« Reply #1 on: October 25, 2013, 07:46:51 AM »
Hi Derkoi,

What you'll need to do is gain access to the tracked camera, and then write your own switch after you instantiate your second camera.  First you'll need to recast the tracked underwater camera so you can access it, change line 14 of the waterModule_underwaterEffect script to be...
Code: [Select]
public var setCamera : GameObject;
Now the camera is accessible and you'll be able to write a switch.  I wrote a general switch below which you can use as a template...
Code: [Select]
var underwaterComponent : waterModule_underwaterEffect;
var camera1 : Transform;
var camera2 : Transform;
var switchCam : boolean = false;

function Start(){
underwaterComponent = GameObject.Find("WATER_Module").gameObject.GetComponent(waterModule_underwaterEffect);

}


function Update(){
if (!switchCam){
if (camera1) underwaterComponent.setCamera = camera1.gameObject;
} else {
if (camera2) underwaterComponent.setCamera = camera2.gameObject;
}
}

I Hope the above code helps, let me know if you have any difficulties or any further questions. :)

-Justin

derkoi

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Adding underwater FX to an instantiated camera?
« Reply #2 on: October 25, 2013, 07:55:09 AM »
Thanks, I'll give that a try.

Thinking about it though, I'd only need the underwater effect on the underwater camera and not on the player, as they wont be able to go into the water.

Is that easier to set up?  :P

Chingwa

  • Administrator
  • Hero Member
  • *****
  • Posts: 1587
    • View Profile
    • Tanuki Digital
Re: Adding underwater FX to an instantiated camera?
« Reply #3 on: October 25, 2013, 08:13:30 AM »
Well, sure.  If all you need to do is specify one camera over another, you can do this by using the MainCamera tag on your camera object.  Suimono automatically uses this tag to determine what the trackable camera should be.

So keep your scene cameras set to 'untagged' except for the one you want to have underwater fx... only this one should be set to 'MainCamera'.

derkoi

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Adding underwater FX to an instantiated camera?
« Reply #4 on: October 25, 2013, 08:22:47 AM »
Thanks, and will that work ok when i instantiate the underwater camera? It doesn't need the camera to be present at the scene start?

Chingwa

  • Administrator
  • Hero Member
  • *****
  • Posts: 1587
    • View Profile
    • Tanuki Digital
Re: Adding underwater FX to an instantiated camera?
« Reply #5 on: October 25, 2013, 08:37:04 AM »
Ahh, that's a great point...  In the current version this check is only performed at Start.

I suggest using the code above to explicitly define what the camera will be.  I edited the code to be simpler for your needs.  Just incorporate it into the code you're using after instantiating your new camera.
Code: [Select]
var underwaterComponent : waterModule_underwaterEffect;
var underCam : camera;


function Start(){
underwaterComponent = GameObject.Find("WATER_Module").gameObject.GetComponent(waterModule_underwaterEffect);
}


function Update(){
if (underCam != null) underwaterComponent.setCamera = underCam.gameObject;
}

"underCam" being your new camera object...
« Last Edit: October 25, 2013, 08:38:44 AM by Chingwa »

derkoi

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Adding underwater FX to an instantiated camera?
« Reply #6 on: October 25, 2013, 08:50:33 AM »
Thanks, I'm getting an error though:

Quote
BCE0120: 'waterModule_underwaterEffect.setCamera' is inaccessible due to its protection level.

Chingwa

  • Administrator
  • Hero Member
  • *****
  • Posts: 1587
    • View Profile
    • Tanuki Digital
Re: Adding underwater FX to an instantiated camera?
« Reply #7 on: October 25, 2013, 08:54:19 AM »
right... you'll also need to change line 14 of the waterModule_underwaterEffect script as described above... :)
Code: [Select]
public var setCamera : GameObject;

derkoi

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Adding underwater FX to an instantiated camera?
« Reply #8 on: October 25, 2013, 09:15:46 AM »
Thanks again.

It's kind of working now but when the underwater camera is spawned I see the underwater effect from my main camera?

derkoi

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Adding underwater FX to an instantiated camera?
« Reply #9 on: October 25, 2013, 09:18:39 AM »
I see the effect_refract_plane from the other cameras

Chingwa

  • Administrator
  • Hero Member
  • *****
  • Posts: 1587
    • View Profile
    • Tanuki Digital
Re: Adding underwater FX to an instantiated camera?
« Reply #10 on: October 25, 2013, 09:20:27 AM »
are your other cameras' gameobject tags set to "untagged"? 

derkoi

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Adding underwater FX to an instantiated camera?
« Reply #11 on: October 25, 2013, 09:36:50 AM »
I have 2 cameras in my game. 1 player camera. 1 Underwater camera that's instantiated with the players fishing equipment.

The camera is still using the player camera until the player has caught a fish and then releases it, at which point the camera switches to the underwater camera to see the fish swim away.

When i use the script you suggested and instantiate the underwater camera with the players fishing equipment, the players camera see's the effect_refract_plane in front of the underwater camera.

Like this:

https://www.dropbox.com/s/gcl1zza82r5z6pu/Screen%20Shot%202013-10-25%20at%2014.59.34.png

untagging the player camera results in the errors...

Chingwa

  • Administrator
  • Hero Member
  • *****
  • Posts: 1587
    • View Profile
    • Tanuki Digital
Re: Adding underwater FX to an instantiated camera?
« Reply #12 on: October 25, 2013, 10:01:19 AM »
Hmm... Is the underwater camera supposed to replace the main camera when it's on?  If so... when you instantiate the fishing camera are you also setting the fishing camera's queue to be above the main camera's queue...?  something like this...
Code: [Select]
fishingCamera.camera.depth = Camera.main.depth + 1;
OR do you need to see both cameras at the same time?

It appears, that the underwater fx plane is parenting correctly.  If you need both cameras to show at the same time then you may also need to do some layer culling so your main camera doesn't view the underwater plane.  For example, you could...
1) make a new game layer called "Underwater".
2) change the 'effect_refract_plane' objects' layer to "Underwater"
3) then remove "Underwater" from your main camera's culling Mask


derkoi

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Adding underwater FX to an instantiated camera?
« Reply #13 on: October 25, 2013, 10:05:49 AM »
Hi,

I have not touched the depth, i simply deactivate the player camera and activate the underwater camera.

I also noticed that there is no underwater fog, just the refraction on the underwater camera...

Chingwa

  • Administrator
  • Hero Member
  • *****
  • Posts: 1587
    • View Profile
    • Tanuki Digital
Re: Adding underwater FX to an instantiated camera?
« Reply #14 on: October 25, 2013, 10:24:23 AM »
Are you sure your main camera is actually turning off???  The screenshot you posted is from the view of your main camera I assume. 

As for the fog... you might need to force the depthmode of your new underwater camera... like so...
Code: [Select]
fishingCamera.camera.depthTextureMode = DepthTextureMode.Depth;
If you like you could email me a simplified version of your scene and I could take a look at the setup myself.  You can email me directly at konnichiwa@tanukidigital.com
-Justin