using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class WaterInteractive : MonoBehaviour { private Rigidbody _rigidbody; [HideInInspector] public Vector3 deepProbe; private FishSystem fishSystem; [HideInInspector] public BaitStats baitStats; public bool useFxWater = true; public GameObject ripplePrefab; private ParticleSystem smuuggeParticle; [Range(0.1f, 1f)] public float objectDisplacement = 0.1f; public float deepForVelocity = 0.1f; public float waterDisplacment = 2f; private float startDrag; public bool useBouncy; private float bouncePower; private float deepVeloPower; private float veloMag; private Vector3 velo; private Collider m_Collider; private void Start() { fishSystem = Object.FindObjectOfType(); baitStats = GetComponent(); _rigidbody = GetComponent(); startDrag = _rigidbody.drag; m_Collider = GetComponent(); if (useFxWater) { AddRipple(); } } private void Update() { } private void FixedUpdate() { deepProbe = ProbeDeep(); velo = _rigidbody.velocity; velo.y = 0f; veloMag = velo.magnitude; if (useBouncy) { bouncePower = 0.01f + Mathf.PingPong(Time.time * 0.015f, 0.015f); } deepVeloPower = veloMag * Mathf.Abs(deepForVelocity); if (deepProbe.y > 0f) { _rigidbody.drag = startDrag * 3f; base.transform.position = Vector3.MoveTowards(base.transform.position, new Vector3(base.transform.position.x, deepProbe.z + bouncePower, base.transform.position.z), Time.deltaTime * (waterDisplacment * objectDisplacement)); _rigidbody.AddForce(Vector3.up * deepForVelocity * deepVeloPower, ForceMode.Acceleration); } else { _rigidbody.drag = startDrag; } ShowWaterFX(); } private Vector3 ProbeDeep() { int mask = LayerMask.GetMask("Terrain"); Vector3 zero = Vector3.zero; if (Physics.Raycast(base.transform.position, -Vector3.up, out var hitInfo, float.PositiveInfinity, mask)) { zero.x = hitInfo.distance; } int mask2 = LayerMask.GetMask("WaterObject"); if (Physics.Raycast(base.transform.position, Vector3.up, out var hitInfo2, float.PositiveInfinity, mask2)) { zero.y = hitInfo2.distance; zero.z = hitInfo2.point.y; } return zero; } private void AddRipple() { if (!(ripplePrefab == null)) { GameObject gameObject = Object.Instantiate(ripplePrefab, base.transform); gameObject.transform.localPosition = Vector3.zero; gameObject.transform.localEulerAngles = Vector3.zero; smuuggeParticle = gameObject.GetComponent(); } } private void ShowWaterFX() { if (smuuggeParticle == null) { return; } if (base.transform.position.y >= deepProbe.z - GetMinDeep() * 6f && deepProbe.z + GetMinDeep() * 5f >= base.transform.position.y) { smuuggeParticle.gameObject.transform.localScale = Vector3.one * (0.1f + base.transform.localScale.y * 0.02f); if (!smuuggeParticle.isPlaying) { smuuggeParticle.Play(); } } else if (smuuggeParticle.isPlaying) { smuuggeParticle.Stop(); } } private float GetMinDeep() { return m_Collider.bounds.size.y / 4f; } public void ForceByWaterCurrent(Vector3 direction, float powerForce) { if (deepProbe.z > base.transform.position.y) { _rigidbody.AddForce(direction * powerForce, ForceMode.Acceleration); } } private void OnTriggerEnter(Collider other) { } private void OnTriggerExit(Collider other) { } private void OnDisable() { _ = fishSystem == null; } }