浮漂2
This commit is contained in:
@@ -193,7 +193,7 @@ Rigidbody:
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 154764976}
|
||||
serializedVersion: 5
|
||||
m_Mass: 0.1
|
||||
m_Mass: 0.01
|
||||
m_LinearDamping: 0
|
||||
m_AngularDamping: 0.05
|
||||
m_CenterOfMass: {x: 0, y: 0, z: 0}
|
||||
@@ -206,7 +206,7 @@ Rigidbody:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_ImplicitCom: 1
|
||||
m_ImplicitTensor: 1
|
||||
m_ImplicitTensor: 0
|
||||
m_UseGravity: 1
|
||||
m_IsKinematic: 0
|
||||
m_Interpolate: 0
|
||||
@@ -232,9 +232,9 @@ CapsuleCollider:
|
||||
m_Enabled: 1
|
||||
serializedVersion: 2
|
||||
m_Radius: 0.004
|
||||
m_Height: 0.08
|
||||
m_Height: 0.04
|
||||
m_Direction: 1
|
||||
m_Center: {x: 0, y: 0.04, z: 0}
|
||||
m_Center: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &154764983
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -247,19 +247,10 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: 976f4f103cc04f34a8a81bd4abba2244, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: Assembly-CSharp::BobberBuoyancyStable
|
||||
waterLevelY: 0
|
||||
enterWaterDepth: 0.003
|
||||
smoothDepth: 0.02
|
||||
buoyancySpring: 100
|
||||
buoyancyDamping: 8
|
||||
maxUpAcceleration: 0
|
||||
extraLinearDampingInWater: 2
|
||||
extraAngularDampingInWater: 2
|
||||
driveCenterOfMassFromCapsule: 0
|
||||
extraCenterOfMassOffset: {x: 0, y: 0, z: 0}
|
||||
rightingTorque: 1.5
|
||||
rightingDamping: 0.5
|
||||
buoyancyOffset: 0.02
|
||||
waterLevel: 0
|
||||
buoyancyMultiplier: 12
|
||||
dampingFactor: 0.5
|
||||
showDebugInfo: 1
|
||||
--- !u!1 &203844586
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -2436,7 +2427,6 @@ GameObject:
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 1568490075}
|
||||
- component: {fileID: 1568490076}
|
||||
m_Layer: 0
|
||||
m_Name: _body
|
||||
m_TagString: Untagged
|
||||
@@ -2459,29 +2449,6 @@ Transform:
|
||||
m_Children: []
|
||||
m_Father: {fileID: 2058458420}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: -0, z: -0}
|
||||
--- !u!136 &1568490076
|
||||
CapsuleCollider:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1568490074}
|
||||
m_Material: {fileID: 0}
|
||||
m_IncludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_ExcludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_LayerOverridePriority: 0
|
||||
m_IsTrigger: 0
|
||||
m_ProvidesContacts: 0
|
||||
m_Enabled: 0
|
||||
serializedVersion: 2
|
||||
m_Radius: 0.0034
|
||||
m_Height: 0.04
|
||||
m_Direction: 1
|
||||
m_Center: {x: 0, y: 0, z: 0}
|
||||
--- !u!1 &1627634077
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -2887,7 +2854,7 @@ Transform:
|
||||
m_GameObject: {fileID: 2058458419}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalPosition: {x: 0, y: -0.04, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children:
|
||||
|
||||
@@ -5,179 +5,144 @@
|
||||
[RequireComponent(typeof(CapsuleCollider))]
|
||||
public class BobberBuoyancyStable : MonoBehaviour
|
||||
{
|
||||
[Header("Water")]
|
||||
public float waterLevelY = 0f;
|
||||
[Header("Water Physics")] public float waterLevel = 0f;
|
||||
|
||||
[Tooltip("必须至少浸入这么深才开始产生浮力(防止还没入水就被顶)")]
|
||||
public float enterWaterDepth = 0.003f; // 3mm(按你的尺度改)
|
||||
[Tooltip("浮力倍数 (建议1.0-1.5)")] public float buoyancyMultiplier = 1.2f;
|
||||
|
||||
[Tooltip("在这个深度范围内做平滑过渡(越大越软)")]
|
||||
public float smoothDepth = 0.02f;
|
||||
[Tooltip("阻尼系数")] public float dampingFactor = 0.3f;
|
||||
|
||||
[Header("Buoyancy Spring")]
|
||||
public float buoyancySpring = 30f;
|
||||
public float buoyancyDamping = 8f;
|
||||
[Header("Debug")] public bool showDebugInfo = true;
|
||||
|
||||
[Tooltip("最大上浮加速度限制(0=不限制)")]
|
||||
public float maxUpAcceleration = 0f;
|
||||
|
||||
[Header("Water Drag")]
|
||||
public float extraLinearDampingInWater = 2f;
|
||||
public float extraAngularDampingInWater = 2f;
|
||||
|
||||
[Header("Center Of Mass")]
|
||||
public bool driveCenterOfMassFromCapsule = true;
|
||||
public Vector3 extraCenterOfMassOffset = new Vector3(0f, -0.01f, 0f); // 恢复原来的重心下移
|
||||
|
||||
[Header("Righting")]
|
||||
public float rightingTorque = 3f; // 适中的归正扭矩
|
||||
public float rightingDamping = 0.8f;
|
||||
|
||||
Rigidbody rb;
|
||||
CapsuleCollider cap;
|
||||
|
||||
float airLinearDamping;
|
||||
float airAngularDamping;
|
||||
private Rigidbody rb;
|
||||
private CapsuleCollider capsule;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
rb = GetComponent<Rigidbody>();
|
||||
cap = GetComponent<CapsuleCollider>();
|
||||
capsule = GetComponent<CapsuleCollider>();
|
||||
rb.useGravity = true;
|
||||
|
||||
airLinearDamping = rb.linearDamping;
|
||||
airAngularDamping = rb.angularDamping;
|
||||
|
||||
ApplyCenterOfMass();
|
||||
rb.maxAngularVelocity = 50f;
|
||||
// 移除了强制设置物理参数的代码,保留用户在Inspector中的设置
|
||||
if (showDebugInfo)
|
||||
{
|
||||
Debug.Log($"Initial mass: {rb.mass}kg, Volume: {CalculateTotalVolume():F6}m³");
|
||||
}
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
ApplyCenterOfMass();
|
||||
ApplyCorrectedBuoyancy();
|
||||
}
|
||||
|
||||
Bounds b = cap.bounds;
|
||||
float bottomY = b.min.y;
|
||||
float topY = b.max.y;
|
||||
void ApplyCorrectedBuoyancy()
|
||||
{
|
||||
// 使用本地坐标系计算边界,支持任意旋转角度
|
||||
Vector3 localUp = transform.up; // 获取物体的本地向上向量
|
||||
Vector3 localDown = -localUp;
|
||||
|
||||
// 用"底部点"判定是否真正入水
|
||||
float bottomSubmersion = waterLevelY - bottomY;
|
||||
if (bottomSubmersion <= enterWaterDepth)
|
||||
{
|
||||
rb.linearDamping = airLinearDamping;
|
||||
rb.angularDamping = airAngularDamping;
|
||||
return;
|
||||
}
|
||||
// 正确获取世界坐标下的胶囊体边界(考虑旋转)
|
||||
Vector3 worldBottom = transform.TransformPoint(capsule.center +
|
||||
localDown * (capsule.height * 0.5f));
|
||||
Vector3 worldTop = transform.TransformPoint(capsule.center +
|
||||
localUp * (capsule.height * 0.5f));
|
||||
|
||||
// 进入水中:阻尼随浸入增强
|
||||
float w = Smooth01((bottomSubmersion - enterWaterDepth) / Mathf.Max(1e-4f, smoothDepth));
|
||||
rb.linearDamping = airLinearDamping + extraLinearDampingInWater * w;
|
||||
rb.angularDamping = airAngularDamping + extraAngularDampingInWater * w;
|
||||
float bottomY = worldBottom.y;
|
||||
float topY = worldTop.y;
|
||||
float totalHeight = Mathf.Abs(topY - bottomY); // 使用绝对值确保高度为正
|
||||
|
||||
// 关键修正:正确的浮力计算
|
||||
float volume = Mathf.PI * cap.radius * cap.radius * cap.height;
|
||||
float submergedVolume = volume * Mathf.Clamp01(bottomSubmersion / cap.height);
|
||||
|
||||
// 浮力 = 排开液体重量 = 体积 × 密度 × 重力
|
||||
float buoyantForce = submergedVolume * 1000f * 9.81f;
|
||||
|
||||
// 物体重量
|
||||
// 计算浸入深度
|
||||
float immersionDepth = Mathf.Max(0, waterLevel - bottomY);
|
||||
|
||||
if (immersionDepth <= 0) return;
|
||||
|
||||
// 计算浸入比例
|
||||
float immersionRatio = Mathf.Clamp01(immersionDepth / totalHeight);
|
||||
|
||||
// 计算总重量
|
||||
float weight = rb.mass * 9.81f;
|
||||
|
||||
// 净浮力(浮力 - 重量)
|
||||
float netBuoyancy = buoyantForce - weight;
|
||||
|
||||
// 添加弹簧阻尼系统
|
||||
float velocity = Vector3.Dot(rb.linearVelocity, Vector3.up);
|
||||
float springForce = buoyancySpring * bottomSubmersion;
|
||||
float dampingForce = buoyancyDamping * velocity;
|
||||
|
||||
float totalForce = netBuoyancy + springForce - dampingForce;
|
||||
totalForce *= w; // 平滑过渡
|
||||
|
||||
// 限制向上的力
|
||||
if (totalForce < 0) totalForce = 0;
|
||||
|
||||
// 限制最大加速度
|
||||
if (maxUpAcceleration > 0f)
|
||||
|
||||
// 计算排水体积(关键修正)
|
||||
float totalVolume = CalculateTotalVolume();
|
||||
float displacedVolume = totalVolume * immersionRatio;
|
||||
if (displacedVolume < 0)
|
||||
{
|
||||
float maxForce = rb.mass * maxUpAcceleration;
|
||||
if (totalForce > maxForce) totalForce = maxForce;
|
||||
Debug.LogError($"入水深度异常={displacedVolume}");
|
||||
displacedVolume = 0;
|
||||
}
|
||||
|
||||
// 计算浮力
|
||||
float buoyantForce = displacedVolume * 1000f * 9.81f * buoyancyMultiplier;
|
||||
|
||||
// 净浮力 = 浮力 - 重量
|
||||
float netForce = buoyantForce - weight;
|
||||
|
||||
// 浮力作用点
|
||||
float buoyY = Mathf.Min(waterLevelY - 0.001f, topY);
|
||||
buoyY = Mathf.Max(buoyY, bottomY);
|
||||
Vector3 buoyPoint = new Vector3(b.center.x, buoyY, b.center.z);
|
||||
float buoyancyY = bottomY + (immersionDepth * 0.5f);
|
||||
Vector3 buoyancyPoint = new Vector3(transform.position.x, buoyancyY, transform.position.z);
|
||||
|
||||
rb.AddForceAtPosition(Vector3.up * totalForce, buoyPoint, ForceMode.Force);
|
||||
// 应用力
|
||||
rb.AddForceAtPosition(Vector3.up * netForce, buoyancyPoint, ForceMode.Force);
|
||||
|
||||
// 简化的归正扭矩系统
|
||||
SimpleRightingSystem(w);
|
||||
}
|
||||
// 添加阻尼稳定浮动
|
||||
Vector3 velocity = rb.GetPointVelocity(buoyancyPoint);
|
||||
Vector3 dampingForce = -velocity * dampingFactor * immersionRatio;
|
||||
rb.AddForceAtPosition(dampingForce, buoyancyPoint, ForceMode.Force);
|
||||
|
||||
void SimpleRightingSystem(float weight)
|
||||
{
|
||||
Vector3 up = transform.up;
|
||||
Vector3 axis = Vector3.Cross(up, Vector3.up);
|
||||
float mag = axis.magnitude;
|
||||
|
||||
if (mag > 1e-4f)
|
||||
if (showDebugInfo)
|
||||
{
|
||||
axis /= mag;
|
||||
float angle = Mathf.Asin(Mathf.Clamp(mag, -1f, 1f)) * Mathf.Rad2Deg;
|
||||
float angVelOnAxis = Vector3.Dot(rb.angularVelocity, axis);
|
||||
|
||||
// 归正扭矩
|
||||
float torque = (rightingTorque * angle - rightingDamping * angVelOnAxis) * weight;
|
||||
rb.AddTorque(axis * torque, ForceMode.Acceleration);
|
||||
Debug.Log($"Immersion: {immersionRatio:P1}, " +
|
||||
$"DispVol: {displacedVolume:F6}m³, " +
|
||||
$"Buoyancy: {buoyantForce:F2}N, " +
|
||||
$"NetForce: {netForce:F2}N");
|
||||
}
|
||||
}
|
||||
|
||||
void ApplyCenterOfMass()
|
||||
float CalculateTotalVolume()
|
||||
{
|
||||
if (!driveCenterOfMassFromCapsule) return;
|
||||
rb.centerOfMass = cap.center + extraCenterOfMassOffset;
|
||||
// 胶囊体总体积 = 圆柱体 + 两个半球
|
||||
float radius = capsule.radius;
|
||||
float cylinderHeight = capsule.height - (2 * radius); // 减去两端球冠
|
||||
|
||||
// 圆柱体积
|
||||
float cylinderVolume = Mathf.PI * radius * radius * cylinderHeight;
|
||||
|
||||
// 两个半球 = 一个完整球体
|
||||
float sphereVolume = (4f / 3f) * Mathf.PI * Mathf.Pow(radius, 3);
|
||||
|
||||
float totalVolume = cylinderVolume + sphereVolume;
|
||||
|
||||
return totalVolume;
|
||||
}
|
||||
|
||||
static float Smooth01(float t)
|
||||
{
|
||||
t = Mathf.Clamp01(t);
|
||||
return t * t * (3f - 2f * t);
|
||||
}
|
||||
|
||||
void OnDrawGizmos()
|
||||
{
|
||||
if (cap == null) return;
|
||||
|
||||
Gizmos.color = Color.blue;
|
||||
Bounds bounds = cap.bounds;
|
||||
|
||||
// 绘制浸入部分
|
||||
float bottomY = bounds.min.y;
|
||||
float submergedHeight = Mathf.Max(0, waterLevelY - bottomY);
|
||||
|
||||
Vector3 submergedCenter = new Vector3(
|
||||
bounds.center.x,
|
||||
bottomY + submergedHeight * 0.5f,
|
||||
bounds.center.z
|
||||
);
|
||||
|
||||
Vector3 submergedSize = new Vector3(
|
||||
bounds.size.x,
|
||||
submergedHeight,
|
||||
bounds.size.z
|
||||
);
|
||||
|
||||
Gizmos.DrawWireCube(submergedCenter, submergedSize);
|
||||
|
||||
// 显示重心
|
||||
if (rb != null)
|
||||
if (capsule == null) return;
|
||||
|
||||
// 显示胶囊体边界
|
||||
Vector3 worldBottom = transform.TransformPoint(capsule.center -
|
||||
Vector3.up * (capsule.height * 0.5f));
|
||||
Vector3 worldTop = transform.TransformPoint(capsule.center +
|
||||
Vector3.up * (capsule.height * 0.5f));
|
||||
|
||||
Gizmos.color = Color.white;
|
||||
Gizmos.DrawLine(worldBottom, worldTop);
|
||||
|
||||
// 显示浸入部分
|
||||
float immersion = Mathf.Max(0, waterLevel - worldBottom.y);
|
||||
if (immersion > 0)
|
||||
{
|
||||
Gizmos.color = Color.red;
|
||||
Vector3 comWorld = transform.TransformPoint(rb.centerOfMass);
|
||||
Gizmos.DrawSphere(comWorld, 0.005f);
|
||||
Gizmos.color = new Color(0, 0, 1, 0.3f);
|
||||
float submergedHeight = Mathf.Min(immersion, capsule.height);
|
||||
Vector3 submergedCenter = new Vector3(
|
||||
transform.position.x,
|
||||
worldBottom.y + submergedHeight * 0.5f,
|
||||
transform.position.z
|
||||
);
|
||||
Gizmos.DrawSphere(submergedCenter, capsule.radius);
|
||||
}
|
||||
|
||||
// 显示水面
|
||||
Gizmos.color = Color.blue;
|
||||
Vector3 waterPos = new Vector3(transform.position.x, waterLevel, transform.position.z);
|
||||
Gizmos.DrawWireSphere(waterPos, 0.05f);
|
||||
}
|
||||
}
|
||||
}
|
||||
198
Assets/Scripts/Test/Buoyancy.cs
Normal file
198
Assets/Scripts/Test/Buoyancy.cs
Normal file
@@ -0,0 +1,198 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using WaveHarmonic.Crest;
|
||||
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class Buoyancy : MonoBehaviour
|
||||
{
|
||||
// public WaterSurface targetSurface;
|
||||
public WaterRenderer _water;
|
||||
|
||||
public bool includeDeformation = true;
|
||||
|
||||
[Tooltip("Approxative radius of object for buoyancy.")]
|
||||
public float sphereRadiusApproximation = 0.25f;
|
||||
|
||||
[Tooltip("Specifies the multiplier for the movement induced by other deformation (waves, swell... etc).")]
|
||||
public float waveForceMultiplier = 1f;
|
||||
|
||||
[Tooltip("Specifies the multiplier for the movement induced by the current of the water surface.")]
|
||||
public float currentSpeedMultiplier = 1f;
|
||||
|
||||
[Tooltip("Specifies the multiplier for the drag forces induced by the viscosity of the mediums.")]
|
||||
public float dragMultiplier = 1f;
|
||||
|
||||
public float defaultRigidbodyDrag = 0.1f;
|
||||
|
||||
public float underwaterRigidbodyAngularDrag = 1f;
|
||||
|
||||
public float overwaterRigidbodyAngularDrag = 0.05f;
|
||||
|
||||
[Tooltip("Specifies the value for surface tension. A high value will stop the object bouncing faster on water.")]
|
||||
public float surfaceTensionDamping = 10f;
|
||||
|
||||
[Tooltip("When enabled, the net force is applied with a random offset to create an angular velocity.")]
|
||||
public bool applyForceWithRandomOffset;
|
||||
|
||||
[Tooltip("When enabled, the height of the custom mesh is taken into account when fetching water surface height.")]
|
||||
public bool isWaterSurfaceACustomMesh;
|
||||
|
||||
[Tooltip(
|
||||
"When enabled, a bunch of gizmos will show showing in blue, the position of the sampling for normals, in magenta the computed normal, in green the direction of the deformation force, in red the direction of the current force, and in a yellow sphere, the approximation volume the buoyancy calculations.")]
|
||||
public bool drawDebug;
|
||||
|
||||
private Vector3 currentDirection;
|
||||
|
||||
// private Vector3 A;
|
||||
//
|
||||
// private Vector3 B;
|
||||
//
|
||||
// private Vector3 C;
|
||||
|
||||
private Vector3 waterPosition;
|
||||
|
||||
private Vector3 normal;
|
||||
|
||||
private Vector3 deformationDirection;
|
||||
|
||||
private Rigidbody rigidbodyComponent;
|
||||
|
||||
private float h;
|
||||
|
||||
private float hNormalized;
|
||||
|
||||
private bool _isEnabled = true;
|
||||
|
||||
readonly SampleFlowHelper _SampleFlowHelper = new();
|
||||
|
||||
private void Start()
|
||||
{
|
||||
rigidbodyComponent = GetComponent<Rigidbody>();
|
||||
rigidbodyComponent.useGravity = false;
|
||||
rigidbodyComponent.linearDamping = defaultRigidbodyDrag;
|
||||
if (_water == null)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
"The variable '_water' needs a valid Water Surface to be assigned for the script to work properly.");
|
||||
}
|
||||
}
|
||||
|
||||
public void EnablePhysics(bool set)
|
||||
{
|
||||
_isEnabled = set;
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!_isEnabled)
|
||||
{
|
||||
h = (hNormalized = 0f);
|
||||
rigidbodyComponent.useGravity = true;
|
||||
}
|
||||
else if (_water != null)
|
||||
{
|
||||
rigidbodyComponent.useGravity = false;
|
||||
FetchWaterSurfaceData(base.transform.position, out waterPosition, out normal, out currentDirection);
|
||||
deformationDirection = Vector3.ProjectOnPlane(normal, Vector3.up);
|
||||
h = Mathf.Clamp(waterPosition.y - (base.transform.position.y - sphereRadiusApproximation), 0f,
|
||||
2f * sphereRadiusApproximation);
|
||||
hNormalized = h * 1f / (2f * sphereRadiusApproximation);
|
||||
float num = MathF.PI * h * h / 3f * (3f * sphereRadiusApproximation - h);
|
||||
rigidbodyComponent.angularDamping = Mathf.Lerp(overwaterRigidbodyAngularDrag,
|
||||
underwaterRigidbodyAngularDrag, hNormalized);
|
||||
Vector3 b = rigidbodyComponent.mass * Physics.gravity;
|
||||
Vector3 vector = Vector3.Lerp(Physics.gravity, b, hNormalized);
|
||||
float num2 = 997f;
|
||||
float num3 = 0.001293f;
|
||||
// float num4 = 1.81E-05f;
|
||||
float num4 = 0.0000181f;
|
||||
float num5 = 0.001f;
|
||||
float num6 = 0.47f;
|
||||
Vector3 vector2 = (0f - num2) * num * Physics.gravity;
|
||||
Vector3 a = MathF.PI * 6f * sphereRadiusApproximation * num4 * -rigidbodyComponent.linearVelocity;
|
||||
Vector3 b2 = MathF.PI * 6f * sphereRadiusApproximation * num5 * -rigidbodyComponent.linearVelocity;
|
||||
Vector3 vector3 = Vector3.Lerp(a, b2, hNormalized) * dragMultiplier;
|
||||
float num7 = Mathf.Lerp(
|
||||
b: Mathf.Sqrt(2f * rigidbodyComponent.mass * (0f - Physics.gravity.y) /
|
||||
(num2 * MathF.PI * Mathf.Pow(sphereRadiusApproximation, 2f) * num6)),
|
||||
a: Mathf.Sqrt(2f * rigidbodyComponent.mass * (0f - Physics.gravity.y) /
|
||||
(num3 * MathF.PI * Mathf.Pow(sphereRadiusApproximation, 2f) * num6)), t: hNormalized);
|
||||
Vector3 force = vector + vector2 + vector3;
|
||||
Vector3 vector4 = (applyForceWithRandomOffset
|
||||
? (new Vector3(UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f),
|
||||
UnityEngine.Random.Range(-1f, 1f)) * sphereRadiusApproximation / 5f)
|
||||
: Vector3.zero);
|
||||
rigidbodyComponent.AddForceAtPosition(force, base.transform.position + vector4, ForceMode.Acceleration);
|
||||
if (hNormalized > 0f && hNormalized < 1f)
|
||||
{
|
||||
Vector3 force2 =
|
||||
-(Vector3.Dot(rigidbodyComponent.linearVelocity, Physics.gravity.normalized) *
|
||||
Physics.gravity.normalized) * surfaceTensionDamping;
|
||||
rigidbodyComponent.AddForce(force2, ForceMode.Acceleration);
|
||||
rigidbodyComponent.AddForce(deformationDirection * waveForceMultiplier);
|
||||
rigidbodyComponent.AddForce(currentDirection * currentSpeedMultiplier);
|
||||
}
|
||||
|
||||
if (rigidbodyComponent.linearVelocity.magnitude > num7)
|
||||
{
|
||||
rigidbodyComponent.linearVelocity = rigidbodyComponent.linearVelocity.normalized * num7;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 FetchWaterSurfaceData(Vector3 point, out Vector3 positionWS, out Vector3 normalWS,
|
||||
out Vector3 currentDirectionWS)
|
||||
{
|
||||
WaterSearchParameters wsp = default(WaterSearchParameters);
|
||||
WaterSearchResult wsr = default(WaterSearchResult);
|
||||
wsp.startPositionWS = wsr.candidateLocationWS;
|
||||
wsp.targetPositionWS = point;
|
||||
wsp.error = 0.01f;
|
||||
wsp.maxIterations = 8;
|
||||
wsp.includeDeformation = includeDeformation;
|
||||
wsp.outputNormal = true;
|
||||
positionWS = wsr.candidateLocationWS;
|
||||
normalWS = Vector3.up;
|
||||
currentDirectionWS = Vector3.right;
|
||||
if (targetSurface.ProjectPointOnWaterSurface(wsp, out wsr))
|
||||
{
|
||||
positionWS = wsr.projectedPositionWS;
|
||||
currentDirectionWS = wsr.currentDirectionWS;
|
||||
normalWS = wsr.normalWS;
|
||||
}
|
||||
|
||||
Vector3 vector = (isWaterSurfaceACustomMesh ? (Vector3.up * _water.transform.position.y) : Vector3.zero);
|
||||
return positionWS + vector;
|
||||
}
|
||||
|
||||
public Vector3 GetCurrentWaterPosition()
|
||||
{
|
||||
return waterPosition;
|
||||
}
|
||||
|
||||
|
||||
public float GetNormalizedHeightOfSphereBelowSurface()
|
||||
{
|
||||
return hNormalized;
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
if (drawDebug)
|
||||
{
|
||||
// Gizmos.color = Color.blue;
|
||||
// Gizmos.DrawSphere(A, sphereRadiusApproximation / 10f);
|
||||
// Gizmos.DrawSphere(B, sphereRadiusApproximation / 10f);
|
||||
// Gizmos.DrawSphere(C, sphereRadiusApproximation / 10f);
|
||||
Gizmos.color = Color.magenta;
|
||||
Gizmos.DrawLine(base.transform.position, base.transform.position + normal);
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawLine(base.transform.position, base.transform.position + deformationDirection * 10f);
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawLine(base.transform.position, base.transform.position + currentDirection);
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawSphere(base.transform.position, sphereRadiusApproximation);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Test/Buoyancy.cs.meta
Normal file
3
Assets/Scripts/Test/Buoyancy.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f273ba33ebe49cd8b31b92db8288a90
|
||||
timeCreated: 1772356495
|
||||
Reference in New Issue
Block a user