You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.1 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace WaterStylizedShader
{
[RequireComponent(typeof(Rigidbody))]
public class FloatingObject : MonoBehaviour
{
public Transform[] floaters;
public float underWaterDrag = 3f;
public float underWaterAngularDrag = 1f;
public float airWaterDrag = 0f;
public float airWaterAngularDrag = 0.05f;
public float floatingPower = 15f;
public float baseWaterHeight = 0f;
public float waterHeightVariation = 2f;
public float waveSpeed = 1.0f;
public float waterHeight;
Rigidbody rb;
int floatersUnderwater;
bool underwater;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
waterHeight = baseWaterHeight + Mathf.Sin(Time.time * waveSpeed) * (waterHeightVariation / 2f);
floatersUnderwater = 0;
for (int i = 0; i < floaters.Length; i++)
{
float diff = floaters[i].position.y - waterHeight;
if (diff < 0)
{
rb.AddForceAtPosition(Vector3.up * floatingPower * Mathf.Abs(diff), floaters[i].position, ForceMode.Force);
floatersUnderwater++;
if (!underwater)
{
underwater = true;
SwitchState(true);
}
}
}
if (underwater && floatersUnderwater == 0)
{
underwater = false;
SwitchState(false);
}
}
void SwitchState(bool isUnderwater)
{
if (isUnderwater)
{
rb.linearDamping = underWaterDrag;
rb.angularDamping = underWaterAngularDrag;
}
else
{
rb.linearDamping = airWaterDrag;
rb.angularDamping = airWaterAngularDrag;
}
}
}
}