130 lines
3.0 KiB
C#
130 lines
3.0 KiB
C#
using UnityEngine;
|
|
|
|
public class WaterDisplacementObject : MonoBehaviour
|
|
{
|
|
public float objectDisplacement = 1f;
|
|
|
|
public float deepForVelocity = 0.1f;
|
|
|
|
public bool useWaterCurrent = true;
|
|
|
|
private float timeToDeepProbe;
|
|
|
|
private float timerDeepProbe;
|
|
|
|
public Vector3 deepProbe;
|
|
|
|
private Rigidbody rigidbody;
|
|
|
|
private float deepVeloPower;
|
|
|
|
private float veloMag;
|
|
|
|
private float startDrag;
|
|
|
|
private FishSystem fishSystem;
|
|
|
|
[HideInInspector]
|
|
public float waterHeightPosition;
|
|
|
|
[HideInInspector]
|
|
public BaitStats baitStats;
|
|
|
|
private Vector3 velo;
|
|
|
|
private void Start()
|
|
{
|
|
rigidbody = GetComponent<Rigidbody>();
|
|
startDrag = rigidbody.drag;
|
|
fishSystem = Object.FindObjectOfType<FishSystem>();
|
|
baitStats = GetComponent<BaitStats>();
|
|
waterHeightPosition = GameObject.FindGameObjectWithTag("Water").transform.position.y;
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
timerDeepProbe -= Time.deltaTime;
|
|
velo = rigidbody.velocity;
|
|
velo.y = 0f;
|
|
veloMag = velo.magnitude;
|
|
deepVeloPower = veloMag * Mathf.Abs(deepForVelocity);
|
|
deepProbe = ProbeDeep();
|
|
if (timerDeepProbe <= 0f)
|
|
{
|
|
timerDeepProbe = timeToDeepProbe;
|
|
}
|
|
if (waterHeightPosition - 0.02f >= base.transform.position.y)
|
|
{
|
|
base.transform.position = Vector3.MoveTowards(base.transform.position, new Vector3(base.transform.position.x, waterHeightPosition - 0.02f, base.transform.position.z), Time.deltaTime * objectDisplacement);
|
|
rigidbody.AddForce(Vector3.up * deepForVelocity * deepVeloPower, ForceMode.Acceleration);
|
|
rigidbody.drag = startDrag * 10f;
|
|
}
|
|
else
|
|
{
|
|
rigidbody.drag = startDrag;
|
|
}
|
|
if (fishSystem != null)
|
|
{
|
|
if (deepProbe.y > 0f)
|
|
{
|
|
fishSystem.AddInwaterObject(this);
|
|
}
|
|
else
|
|
{
|
|
fishSystem.DeleteInwaterObject(this);
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public void ForceByWaterCurrent(Vector3 direction, float powerForce)
|
|
{
|
|
if (useWaterCurrent && deepProbe.z >= base.transform.position.y)
|
|
{
|
|
rigidbody.AddForce(direction * powerForce, ForceMode.Acceleration);
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (!(fishSystem == null))
|
|
{
|
|
fishSystem.DeleteInwaterObject(this);
|
|
}
|
|
}
|
|
|
|
private void BuildSplash(Vector3 contactPosition)
|
|
{
|
|
int num = Random.Range(0, 1);
|
|
GameObject obj = Object.Instantiate(ScriptsHandler.Instance.waterFishSplash[num]);
|
|
obj.transform.position = contactPosition;
|
|
Vector3 vector = Vector3.one * velo.magnitude * base.transform.localScale.x;
|
|
vector = Vector3.ClampMagnitude(vector, 0.1f);
|
|
obj.transform.localScale = vector;
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider col)
|
|
{
|
|
if (col.transform.tag == "Water")
|
|
{
|
|
BuildSplash(base.transform.position);
|
|
}
|
|
}
|
|
}
|