Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- public class ScaleWaveHeightByDistance : MonoBehaviour
- {
- public Transform player;
- public Material waterMaterial;
- [Header("Distance gradient")]
- public float minDistance = 100f;
- public float maxDistance = 500f;
- [Header("Wave scale")]
- [Min(0f)]
- public float minHeight = 0f;
- [Min(0.1f)]
- public float maxHeight = 1f;
- float originalHeight;
- private static readonly int _WaveHeight = Shader.PropertyToID("_WaveHeight");
- void Start()
- {
- if (waterMaterial == null || waterMaterial.HasProperty(_WaveHeight) == false)
- {
- this.enabled = false;
- throw new Exception("Water material is missing or not using the Stylized Water 3 shader...");
- }
- originalHeight = waterMaterial.GetFloat(_WaveHeight);
- }
- private void Update()
- {
- float distance = Vector3.Distance(this.transform.position, player.position);
- float t = (distance - minDistance) / (maxDistance - minDistance);
- waterMaterial.SetFloat(_WaveHeight, Mathf.Lerp(minHeight, maxHeight, t));
- }
- private void OnDestroy()
- {
- waterMaterial.SetFloat(_WaveHeight, originalHeight);
- }
- private void OnDrawGizmosSelected()
- {
- #if UNITY_EDITOR
- UnityEditor.Handles.color = Color.yellow;
- UnityEditor.Handles.DrawWireDisc(this.transform.position, Vector3.up, minDistance);
- UnityEditor.Handles.DrawWireDisc(this.transform.position, Vector3.up, maxDistance);
- #endif
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement