Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace StylizedWater2
- {
- public class FloatingRigidbody : MonoBehaviour
- {
- public new Rigidbody rigidbody;
- [Space]
- [Tooltip("This reference is required to grab the wave distance and height values")]
- public WaterObject waterObject;
- [Tooltip("Automatically find the Water Object below of above the Transform's position. This is slower than assigning a specific Water Object directly.")]
- public bool autoFind = true;
- public Material waterMaterial;
- public enum WaterLevelSource
- {
- FixedValue,
- WaterObject
- }
- [Tooltip("Configure what should be used to set the base water level. Relative wave height is added to this value")]
- public WaterLevelSource waterLevelSource = WaterLevelSource.WaterObject;
- public float waterLevel;
- [Space]
- [Range(0.01f, 1f)]
- public float buoyancy = 0.5f;
- [Range(0.01f, 0.99f)]
- public float drag = 0.99f;
- [Range(0f, 1f)]
- public float horizontalForce = 0f;
- private float m_waterLevel = 0f;
- public Mesh buoyancyShape;
- public List<Vector3> samples;
- private void Reset()
- {
- rigidbody = this.GetComponent<Rigidbody>();
- waterObject = WaterObject.Find(this.transform.position, true);
- if (waterObject)
- {
- waterMaterial = waterObject.material;
- }
- samples.Add(Vector3.zero);
- }
- private void FixedUpdate()
- {
- if(autoFind) waterObject = WaterObject.Find(transform.position, false);
- if (waterObject) waterMaterial = waterObject.material;
- if (!rigidbody || !waterMaterial) return;
- m_waterLevel = waterObject && waterLevelSource == WaterLevelSource.WaterObject? waterObject.transform.position.y : waterLevel;
- for (int i = 0; i < samples.Count; i++)
- {
- Vector3 samplePos = transform.TransformPoint(samples[i]);
- Vector3 waveNormal = Vector3.zero;
- float waveHeight = Buoyancy.SampleWaves(samplePos, waterMaterial, m_waterLevel, 1f, false, out waveNormal);
- float depth = waveHeight - samplePos.y;
- if (depth > 0)
- {
- samplePos.y = waveHeight;
- rigidbody.AddForceAtPosition(Vector3.Lerp(Vector3.up, waveNormal, horizontalForce) * ((Mathf.Abs(Physics.gravity.y)) * buoyancy * rigidbody.mass), samplePos, ForceMode.Acceleration);
- rigidbody.AddForce(-rigidbody.velocity * (drag * Time.fixedDeltaTime), ForceMode.VelocityChange);
- rigidbody.AddTorque(-rigidbody.angularVelocity * (0.5f * Time.fixedDeltaTime), ForceMode.Impulse);
- }
- }
- }
- [ContextMenu("CreatePointsFromBuoyancyShape")]
- public void CreatePointsFromBuoyancyShape()
- {
- if (!buoyancyShape) return;
- Vector3[] vertices = buoyancyShape.vertices;
- samples.Clear();
- for (int i = 0; i < vertices.Length; i++)
- {
- samples.Add(vertices[i]);
- }
- }
- private void OnDrawGizmosSelected()
- {
- if (!waterMaterial) return;
- for (int i = 0; i < samples.Count; i++)
- {
- Vector3 samplePos = transform.TransformPoint(samples[i]);
- float waveHeight = StylizedWater2.Buoyancy.SampleWaves(samplePos, waterMaterial, m_waterLevel, 0f, false, out _);
- Vector3 wavePos = samplePos;
- wavePos.y = waveHeight;
- Gizmos.DrawLine(samplePos, wavePos);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement