|
@ -4,6 +4,16 @@ using UnityEngine; |
|
|
|
|
|
|
|
|
public class WaterController : MonoBehaviour { |
|
|
public class WaterController : MonoBehaviour { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public AnimationCurve RipplePower; |
|
|
|
|
|
|
|
|
|
|
|
private float waveOffset = 0; |
|
|
|
|
|
public float waveScale = 1; |
|
|
|
|
|
public float waterWidth = 1; |
|
|
|
|
|
public float waveSpeed = 1; |
|
|
|
|
|
public float maxWaveHeight = 2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Unity Functions
|
|
|
#region Unity Functions
|
|
|
// Use this for initialization
|
|
|
// Use this for initialization
|
|
|
void Start() { |
|
|
void Start() { |
|
@ -13,6 +23,8 @@ public class WaterController : MonoBehaviour { |
|
|
// Update is called once per frame
|
|
|
// Update is called once per frame
|
|
|
void Update() { |
|
|
void Update() { |
|
|
|
|
|
|
|
|
|
|
|
waveOffset += 0.1f *waveSpeed * Time.deltaTime; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
#endregion Unity Fucntions
|
|
|
#endregion Unity Fucntions
|
|
|
|
|
|
|
|
@ -28,25 +40,56 @@ public class WaterController : MonoBehaviour { |
|
|
public void CreateWave(Vector3 point, float radius, float power){ |
|
|
public void CreateWave(Vector3 point, float radius, float power){ |
|
|
|
|
|
|
|
|
//find all colliders within the wave distance
|
|
|
//find all colliders within the wave distance
|
|
|
Collider[] colliders = Physics.OverlapSphere(point, radius); |
|
|
|
|
|
foreach (Collider hit in colliders) { |
|
|
|
|
|
Rigidbody rb = hit.GetComponent<Rigidbody>(); |
|
|
|
|
|
|
|
|
StartCoroutine(waveController(point, radius, power, 2)); |
|
|
|
|
|
|
|
|
if (rb != null) |
|
|
|
|
|
rb.AddExplosionForce(power, point, radius, 0.0f); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
#endregion Interaction Functions
|
|
|
|
|
|
|
|
|
|
|
|
#region Helper Functions
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerator waveController (Vector3 point, float radius, float power, float rippleTime) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float elapsedTime = 0.0f; |
|
|
|
|
|
|
|
|
|
|
|
while (elapsedTime < rippleTime) { |
|
|
|
|
|
|
|
|
|
|
|
float curRippleForce = RipplePower.Evaluate(elapsedTime / rippleTime); |
|
|
|
|
|
|
|
|
#endregion Interaction Functions
|
|
|
|
|
|
|
|
|
Collider[] colliders = Physics.OverlapSphere(point, radius); |
|
|
|
|
|
foreach (Collider hit in colliders) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Rigidbody rb = hit.GetComponent<Rigidbody>(); |
|
|
|
|
|
|
|
|
|
|
|
Debug.DrawLine(point, hit.transform.position, Color.blue, 1); |
|
|
|
|
|
|
|
|
|
|
|
if (rb != null) { |
|
|
|
|
|
Debug.DrawLine(point, hit.transform.position, Color.blue, 1); |
|
|
|
|
|
Vector3 flatPoint = new Vector3(point.x, hit.transform.position.y, point.z); |
|
|
|
|
|
rb.AddExplosionForce(power * curRippleForce, point, radius, 0.0f); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
yield return new WaitForEndOfFrame(); |
|
|
|
|
|
elapsedTime += Time.deltaTime; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion Helper Functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Collision Functions
|
|
|
#region Collision Functions
|
|
|
void OnTriggerEnter(Collider other) { |
|
|
void OnTriggerEnter(Collider other) { |
|
|
//calls appropriate function if the object should interact with the water on enter
|
|
|
//calls appropriate function if the object should interact with the water on enter
|
|
|
WaterObject waterInteraction = other.gameObject.GetComponent<WaterObject>(); |
|
|
WaterObject waterInteraction = other.gameObject.GetComponent<WaterObject>(); |
|
|
if (waterInteraction != null) { |
|
|
if (waterInteraction != null) { |
|
|
waterInteraction.OnWaterEnter(); |
|
|
|
|
|
|
|
|
waterInteraction.OnWaterEnter(gameObject); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
@ -56,7 +99,13 @@ public class WaterController : MonoBehaviour { |
|
|
//calls appropriate function if the object should interact with the water on stay
|
|
|
//calls appropriate function if the object should interact with the water on stay
|
|
|
WaterObject waterInteraction = other.gameObject.GetComponent<WaterObject>(); |
|
|
WaterObject waterInteraction = other.gameObject.GetComponent<WaterObject>(); |
|
|
if (waterInteraction != null) { |
|
|
if (waterInteraction != null) { |
|
|
waterInteraction.OnWaterStay(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Vector2 waveCoords = new Vector2(other.transform.position.x, other.transform.position.z); |
|
|
|
|
|
waveCoords = waveCoords / waterWidth * waveScale; |
|
|
|
|
|
float waveHeight = Mathf.PerlinNoise(waveCoords.x, waveCoords.y + waveOffset); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
waterInteraction.OnWaterStay(gameObject,waveHeight * maxWaveHeight); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
@ -65,7 +114,7 @@ public class WaterController : MonoBehaviour { |
|
|
//calls appropriate function if the object should interact with the water on exit
|
|
|
//calls appropriate function if the object should interact with the water on exit
|
|
|
WaterObject waterInteraction = other.gameObject.GetComponent<WaterObject>(); |
|
|
WaterObject waterInteraction = other.gameObject.GetComponent<WaterObject>(); |
|
|
if (waterInteraction != null) { |
|
|
if (waterInteraction != null) { |
|
|
waterInteraction.OnWaterExit(); |
|
|
|
|
|
|
|
|
waterInteraction.OnWaterExit(gameObject); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|