Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using StylizedWater2;
- using UnityEngine;
- //Component requires to be added to a GameObject with a Collider (trigger checkbox enabled)
- public class UnderwaterTrigger : MonoBehaviour
- {
- private const string CAMERA_TAG = "MainCamera";
- //If enabled, rendering will also be toggled based on triggers
- //This way the camera can go below the (last set) water level without activating rendering (eg. stairs running down, next to a swimming pool)
- public bool toggleRendering = true;
- [Space]
- public Material waterMaterial;
- public float waterLevel;
- public bool useTransfromForWaterlevel;
- private void Start()
- {
- //This is just to ensure that by default the camera is never considered underwater
- UnderwaterRenderer.SetCurrentWaterLevel(-999f);
- //Making sure that the rendering is disabled until the camera first enters a trigger.
- if(toggleRendering) UnderwaterRenderer.EnableRendering = false;
- }
- private void OnTriggerEnter(Collider other)
- {
- //Note that in order for the camera to react to triggers it also needs to have a trigger on its GameObject, as well as a RigidBody component (with Kinematic enabled, so it doesn't fall)
- if (!other.CompareTag(CAMERA_TAG)) return;
- if(!useTransfromForWaterlevel)
- {
- UnderwaterRenderer.SetCurrentWaterLevel(waterLevel);
- }
- //Or use the transform's Y-position
- else
- {
- UnderwaterRenderer.SetCurrentWaterLevel(this.transform);
- }
- //You can leave the material field empty, in case every water body uses the same material anyway, just at different water levels
- if(waterMaterial) UnderwaterRenderer.SetCurrentWaterMaterial(waterMaterial);
- if (toggleRendering) UnderwaterRenderer.EnableRendering = true;
- }
- private void OnTriggerExit(Collider other)
- {
- if (!other.CompareTag(CAMERA_TAG)) return;
- if (toggleRendering) UnderwaterRenderer.EnableRendering = false;
- }
- }
Add Comment
Please, Sign In to add comment