adding realistic water

This commit is contained in:
2023-06-22 18:48:08 +03:00
parent b2cf8fde92
commit e1b4e4062f
5 changed files with 201 additions and 2 deletions
-2
View File
@@ -1,4 +1,3 @@
using System;
using UnityEngine;
public class Player : MonoBehaviour
@@ -102,7 +101,6 @@ public class Player : MonoBehaviour
return Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, .1f, ladderLayer);
}
private void FixedUpdate()
{
+130
View File
@@ -0,0 +1,130 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
public class WaterShapeController : MonoBehaviour
{
[SerializeField]
private GameObject wavePointPref;
[SerializeField]
private float _springStiffness = 0.1f;
[SerializeField]
private float _dampening = 0.3f;
[SerializeField]
private List<WaterSpring> _springs = new List<WaterSpring>();
[SerializeField]
private SpriteShapeController _spriteShapeController;
[SerializeField]
private int _wavesCount = 6;
[SerializeField]
private GameObject _wavePoints;
private int _cornersCount = 2;
private float _spread = 0.006f;
private void SetWaves()
{
Spline waterSpline = _spriteShapeController.spline;
var waterPointsCount = waterSpline.GetPointCount();
for (int count = _cornersCount; count < waterPointsCount - _cornersCount; count++)
{
waterSpline.RemovePointAt(_cornersCount);
}
Vector3 waterTopLeftCorner = waterSpline.GetPosition(1);
Vector3 waterTopRightCorner = waterSpline.GetPosition(2);
float waterWidth = waterTopRightCorner.x - waterTopLeftCorner.x;
float spacingPerWawe = waterWidth / (_wavesCount + 1);
for (int count = _wavesCount; count > 0; count--)
{
int index = _cornersCount;
float xPosition=waterTopLeftCorner.x+(spacingPerWawe * count);
Vector3 wavePoint=new Vector3(xPosition,waterTopLeftCorner.y,waterTopLeftCorner.z);
waterSpline.InsertPointAt(index, wavePoint );
waterSpline.SetHeight(index, 0.1f);
waterSpline.SetCorner(index, false);
}
CreateSprings(waterSpline);
Splash(2, 1);
}
private void CreateSprings(Spline waterSpline)
{
_springs = new List<WaterSpring>();
for(int idx=0;idx<_wavesCount+1; idx++)
{
int index = idx + 1;
GameObject wavePoint = Instantiate(wavePointPref, _wavePoints.transform, false);
wavePoint.transform.localPosition = waterSpline.GetPosition(index);
WaterSpring waterSpring=wavePoint.GetComponent<WaterSpring>();
waterSpring.Init(_spriteShapeController);
_springs.Add( waterSpring );
}
}
private void UpdateSprings()
{
int springsCount = _springs.Count;
float[] left_deltas = new float[springsCount];
float[] right_deltas = new float[springsCount];
for (int index = 0; index < springsCount; index++)
{
if (index > 0)
{
left_deltas[index] = _spread * (_springs[index].Height - _springs[index - 1].Height);
_springs[index - 1].Velocity += left_deltas[index];
}
if (index <springsCount-1)
{
right_deltas[index] = _spread * (_springs[index].Height - _springs[index + 1].Height);
_springs[index + 1].Velocity += right_deltas[index];
}
}
}
private void Splash(int index,float speed)
{
if(index>=0 && index<_springs.Count) {
_springs[index].Velocity += speed;
}
}
// On Enable for example purposes
void OnEnable()
{
StartCoroutine(CreateWaves());
foreach (WaterSpring waterSpringComponent in _springs)
{
waterSpringComponent.Init(_spriteShapeController);
}
}
IEnumerator CreateWaves()
{
foreach (Transform child in _wavePoints.transform)
{
StartCoroutine(Destroy(child.gameObject));
}
yield return null;
SetWaves();
yield return null;
}
IEnumerator Destroy(GameObject go)
{
yield return null;
DestroyImmediate(go);
}
private void FixedUpdate()
{
foreach (WaterSpring spring in _springs)
{
spring.WaveSpringUpdate(_springStiffness, _dampening);
}
UpdateSprings();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3ed3c1e5ea4ca4f41a13946462533c67
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+49
View File
@@ -0,0 +1,49 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
public class WaterSpring : MonoBehaviour
{
public float Velocity { get; set; } = 0f;
private float _force = 0f;
public float Height { get; set; } = 0f;
private float _targetHeight = 0f;
private int _waveIndex;
private SpriteShapeController _spriteShapeController;
public void Init(SpriteShapeController spriteShapeController)
{
_spriteShapeController = spriteShapeController;
var index=transform.GetSiblingIndex();
_waveIndex = index + 1;
Velocity = 0;
Height = transform.localPosition.y;
Height = transform.localPosition.y;
}
public void WavePointUpdate(int level)
{
if (_spriteShapeController != null)
{
Spline waterSpline = _spriteShapeController.spline;
Vector3 wavePosition = waterSpline.GetPosition(_waveIndex);
waterSpline.SetPosition(_waveIndex, new Vector3(wavePosition.x, transform.localPosition.y, wavePosition.z));
}
}
public void WaveSpringUpdate(float springStiffness, float dampening)
{
Height = transform.localPosition.y;
var maximumExtension = Height - _targetHeight;
var loss = -dampening * Velocity;
_force = -springStiffness * maximumExtension+loss;
Velocity +=_force;
var y=transform.localPosition.y;
transform.localPosition = new Vector3(transform.position.x, y+Velocity, transform.position.z);
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bf31330d7df081c4c926958352c5fd98
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: