81 lines
1.4 KiB
C#
81 lines
1.4 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering.HighDefinition;
|
|
|
|
namespace UnityWaterTools
|
|
{
|
|
public class WaterFloater : MonoBehaviourExtended
|
|
{
|
|
public Rigidbody rbody;
|
|
|
|
public float depthBefSub;
|
|
|
|
public bool useDisplacement;
|
|
|
|
public float displacementAmount = 1f;
|
|
|
|
public int floaters;
|
|
|
|
public float waterDrag;
|
|
|
|
public float waterAngularDrag;
|
|
|
|
private WaterSearchParameters waterSearchParameters;
|
|
|
|
private WaterSearchResult waterSearchResult;
|
|
|
|
public WaterSurface water;
|
|
|
|
private bool inWater;
|
|
|
|
public bool InWater
|
|
{
|
|
get
|
|
{
|
|
return inWater;
|
|
}
|
|
private set
|
|
{
|
|
if (value != inWater)
|
|
{
|
|
this.OnWaterEnter?.Invoke(value);
|
|
inWater = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public event Action<bool> OnWaterEnter;
|
|
|
|
private void Start()
|
|
{
|
|
SetToWaterSurface();
|
|
}
|
|
|
|
private void SetToWaterSurface()
|
|
{
|
|
water = MonoBehaviourSingleton<GameWater>.Instance.WaterSurface;
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if ((object)water != null)
|
|
{
|
|
waterSearchParameters.startPositionWS = base.transform.position;
|
|
water.ProjectPointOnWaterSurface(waterSearchParameters, out waterSearchResult);
|
|
if (base.transform.position.y < waterSearchResult.projectedPositionWS.y)
|
|
{
|
|
InWater = true;
|
|
}
|
|
else
|
|
{
|
|
InWater = false;
|
|
}
|
|
if (useDisplacement && inWater)
|
|
{
|
|
rbody.AddForce(Vector3.up * displacementAmount, ForceMode.Force);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|