测试修改提交

This commit is contained in:
2026-03-04 09:18:04 +08:00
parent 441f12b616
commit 0de35591e7
9 changed files with 256 additions and 174 deletions

View File

@@ -26,7 +26,7 @@ RenderSettings:
m_AmbientIntensity: 0.8050667
m_AmbientMode: 0
m_SubtractiveShadowColor: {r: 0.8679245, g: 0.8679245, b: 0.8679245, a: 1}
m_SkyboxMaterial: {fileID: 16041075}
m_SkyboxMaterial: {fileID: 1948842527}
m_HaloStrength: 0.5
m_FlareStrength: 1
m_FlareFadeSpeed: 3
@@ -119,54 +119,6 @@ NavMeshSettings:
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
--- !u!21 &16041075
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Enviro/Skybox
m_Shader: {fileID: 4800000, guid: 34fee0a1b19b20b45aea7483b5f757da, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords: []
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _GalaxyTex:
m_Texture: {fileID: 8900000, guid: 5734983fc81450b4187c3cfa5985edef, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MoonGlowTex:
m_Texture: {fileID: 2800000, guid: 6838e0810da4e49488b5d9a6ee76eb07, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MoonTex:
m_Texture: {fileID: 2800000, guid: c6fd9f694390e0245b6dca5812065950, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _StarsTex:
m_Texture: {fileID: 8900000, guid: b5a7175da0f133b4d951c19c9c2cebfc, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SunTex:
m_Texture: {fileID: 2800000, guid: c95bed5306e94f24ba5802d841607ac7, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats: []
m_Colors: []
m_BuildTextureStacks: []
m_AllowLocking: 1
--- !u!1 &116710580
GameObject:
m_ObjectHideFlags: 0
@@ -1488,6 +1440,54 @@ MonoBehaviour:
_MaximumSpeed: 100
_WarnOnSpeedClamp: 0
_DebugSubsteps: 0
--- !u!21 &1948842527
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Enviro/Skybox
m_Shader: {fileID: 4800000, guid: 34fee0a1b19b20b45aea7483b5f757da, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords: []
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _GalaxyTex:
m_Texture: {fileID: 8900000, guid: 5734983fc81450b4187c3cfa5985edef, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MoonGlowTex:
m_Texture: {fileID: 2800000, guid: 6838e0810da4e49488b5d9a6ee76eb07, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MoonTex:
m_Texture: {fileID: 2800000, guid: c6fd9f694390e0245b6dca5812065950, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _StarsTex:
m_Texture: {fileID: 8900000, guid: b5a7175da0f133b4d951c19c9c2cebfc, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SunTex:
m_Texture: {fileID: 2800000, guid: c95bed5306e94f24ba5802d841607ac7, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats: []
m_Colors: []
m_BuildTextureStacks: []
m_AllowLocking: 1
--- !u!1 &2034802495
GameObject:
m_ObjectHideFlags: 0
@@ -8268,7 +8268,7 @@ MonoBehaviour:
WaterObject: {fileID: 718680767}
Node: {fileID: 3673832994973231279}
GearNode: {fileID: 6022323663597824692}
Water: {fileID: 0}
Water: {fileID: 718680768}
LineRenderer: {fileID: 0}
FPS: 0
updateInterval: 0.2

View File

@@ -454,7 +454,7 @@ GameObject:
- component: {fileID: 54298866000586118}
- component: {fileID: 153691655494134957}
- component: {fileID: 2717383850592950062}
- component: {fileID: 3104700584461846010}
- component: {fileID: 8545743762334745476}
m_Layer: 16
m_Name: Float
m_TagString: Untagged
@@ -620,29 +620,56 @@ MonoBehaviour:
m_EditorClassIdentifier: Assembly-CSharp::NBF.BobberController
_rbody: {fileID: 54298866000586118}
joint: {fileID: 153691655494134957}
_buoyancy: {fileID: 3104700584461846010}
--- !u!114 &3104700584461846010
--- !u!114 &8545743762334745476
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1858052053854210}
m_Enabled: 0
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: dd1aa9f0de8b435448c6893ecc60d021, type: 3}
m_Script: {fileID: 11500000, guid: f91c9d873c83492ca6d5e3e3a67c1760, type: 3}
m_Name:
m_EditorClassIdentifier: Assembly-CSharp::Buoyancy
includeDeformation: 1
sphereRadiusApproximation: 0.1
waveForceMultiplier: 0.01
currentSpeedMultiplier: 0.01
dragMultiplier: 1
defaultRigidbodyDrag: 5
underwaterRigidbodyAngularDrag: 5
overwaterRigidbodyAngularDrag: 1
surfaceTensionDamping: 10
applyForceWithRandomOffset: 0
m_EditorClassIdentifier: Assembly-CSharp::CapsuleBuoyancyStable
buoyancyScale: 1.6
samplePoints: 9
submergenceCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 0
inSlope: 0
outSlope: 1
tangentMode: 0
weightedMode: 0
inWeight: 0
outWeight: 0
- serializedVersion: 3
time: 1
value: 1
inSlope: 1
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0
outWeight: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
verticalDamping: 0.6
angularDamping: 0.1
uprightSpring: 0
uprightDamping: 0.5
uprightAxis: 1
extraDragInWater: 0.8
extraAngularDragInWater: 0.3
nearUprightDampingReduce: 0.6
nearUprightAngleDeg: 12
_waterRenderer: {fileID: 0}
_Layer: 1
_ObjectWidth: 3
drawDebug: 1
--- !u!1 &1933124697579601
GameObject:

View File

@@ -70,10 +70,10 @@ CapsuleCollider:
m_ProvidesContacts: 0
m_Enabled: 1
serializedVersion: 2
m_Radius: 0.002
m_Height: 0.03
m_Radius: 0.003
m_Height: 0.02
m_Direction: 1
m_Center: {x: 0, y: 0.01, z: 0}
m_Center: {x: 0, y: 0.03, z: 0}
--- !u!1 &1236424003946422
GameObject:
m_ObjectHideFlags: 0
@@ -367,7 +367,7 @@ Transform:
m_GameObject: {fileID: 1696436611728297872}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: -0.04, z: 0}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:

View File

@@ -1177,6 +1177,7 @@ MonoBehaviour:
topConnector: {fileID: 209664165}
bottomConnector: {fileID: 332592725}
waterline: {fileID: 223862664}
collider: {fileID: 0}
--- !u!1 &920563739
GameObject:
m_ObjectHideFlags: 0
@@ -2519,6 +2520,7 @@ MonoBehaviour:
topConnector: {fileID: 1549463780}
bottomConnector: {fileID: 726690746}
waterline: {fileID: 1627634078}
collider: {fileID: 0}
--- !u!1 &1690494754
GameObject:
m_ObjectHideFlags: 0
@@ -2641,6 +2643,7 @@ MonoBehaviour:
topConnector: {fileID: 1224158073}
bottomConnector: {fileID: 1336541387}
waterline: {fileID: 666518409}
collider: {fileID: 0}
--- !u!4 &1866024594 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 8356280719142672529, guid: 84f17dfc7c7a7485296643a4e64d6200, type: 3}
@@ -2999,6 +3002,7 @@ MonoBehaviour:
topConnector: {fileID: 1828830192}
bottomConnector: {fileID: 1690494755}
waterline: {fileID: 623951639}
collider: {fileID: 0}
--- !u!1 &1586000250375033
GameObject:
m_ObjectHideFlags: 0
@@ -3041,6 +3045,7 @@ GameObject:
m_Component:
- component: {fileID: 4046714554611455}
- component: {fileID: 309487077947173374}
- component: {fileID: 309487077947173375}
m_Layer: 0
m_Name: bob_25002
m_TagString: Untagged
@@ -3394,6 +3399,30 @@ MonoBehaviour:
topConnector: {fileID: 4466248854207531}
bottomConnector: {fileID: 4064788320456926}
waterline: {fileID: 4902566405131541}
collider: {fileID: 309487077947173375}
--- !u!136 &309487077947173375
CapsuleCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1818733047993069}
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: 1
serializedVersion: 2
m_Radius: 0.004
m_Height: 0.025
m_Direction: 1
m_Center: {x: 0, y: 0.04, z: 0}
--- !u!4 &389927712696538647
Transform:
m_ObjectHideFlags: 0
@@ -4665,6 +4694,7 @@ MonoBehaviour:
topConnector: {fileID: 1159196036679945669}
bottomConnector: {fileID: 4333116976202729697}
waterline: {fileID: 7961972470609608556}
collider: {fileID: 0}
--- !u!1 &3062767698477047043
GameObject:
m_ObjectHideFlags: 0
@@ -5081,6 +5111,7 @@ MonoBehaviour:
topConnector: {fileID: 3065647928701135441}
bottomConnector: {fileID: 3065526845897688740}
waterline: {fileID: 3068616352015752047}
collider: {fileID: 0}
--- !u!54 &3372919389234347911
Rigidbody:
m_ObjectHideFlags: 0
@@ -5606,6 +5637,7 @@ MonoBehaviour:
topConnector: {fileID: 9066712976035742038}
bottomConnector: {fileID: 5901816832231670898}
waterline: {fileID: 269017554130809855}
collider: {fileID: 0}
--- !u!136 &4959874663812492083
CapsuleCollider:
m_ObjectHideFlags: 0
@@ -6507,6 +6539,8 @@ MonoBehaviour:
uprightAxis: 1
extraDragInWater: 0.8
extraAngularDragInWater: 0.8
nearUprightDampingReduce: 0.6
nearUprightAngleDeg: 12
_waterRenderer: {fileID: 2531380344179187550}
_Layer: 1
_ObjectWidth: 3
@@ -6738,7 +6772,6 @@ GameObject:
m_Component:
- component: {fileID: 8107394097429219735}
- component: {fileID: 8094040829892155629}
- component: {fileID: 8264424693587136968}
- component: {fileID: 8264424693587136966}
- component: {fileID: 6135364979465448005}
- component: {fileID: 8264424693587136969}
@@ -6748,7 +6781,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!1 &8112602444043959218
GameObject:
m_ObjectHideFlags: 0
@@ -7252,29 +7285,6 @@ ConfigurableJoint:
m_EnablePreprocessing: 0
m_MassScale: 1
m_ConnectedMassScale: 1
--- !u!136 &8264424693587136968
CapsuleCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8112599368562234729}
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: 1
serializedVersion: 2
m_Radius: 0.004
m_Height: 0.025
m_Direction: 1
m_Center: {x: 0, y: 0.04, z: 0}
--- !u!114 &8264424693587136969
MonoBehaviour:
m_ObjectHideFlags: 0
@@ -7319,7 +7329,9 @@ MonoBehaviour:
uprightDamping: 0.5
uprightAxis: 1
extraDragInWater: 0.8
extraAngularDragInWater: 0.8
extraAngularDragInWater: 0.3
nearUprightDampingReduce: 0.6
nearUprightAngleDeg: 12
_waterRenderer: {fileID: 2531380344179187550}
_Layer: 1
_ObjectWidth: 3

View File

@@ -24,5 +24,6 @@ namespace NBF
/// 水线
/// </summary>
public Transform waterline;
}
}

View File

@@ -9,6 +9,8 @@ namespace NBF
// transform.position = Rod.lineHandler.LineConnector_1.transform.position;
SetParent(Rod.Line.Bobber.transform);
transform.localPosition = Vector3.zero;
var buoyancy = GetComponentInParent<CapsuleBuoyancyStable>();
buoyancy.InitBobber();
}
}
}

View File

@@ -4,7 +4,7 @@ using UnityEngine;
using WaveHarmonic.Crest;
[DisallowMultipleComponent]
[RequireComponent(typeof(Rigidbody), typeof(CapsuleCollider))]
[RequireComponent(typeof(Rigidbody))]
public class CapsuleBuoyancyStable : MonoBehaviour
{
[Header("Buoyancy")] [Tooltip("完全浸没时总浮力 = mass*g*buoyancyScale。>1 更浮。")]
@@ -15,8 +15,8 @@ public class CapsuleBuoyancyStable : MonoBehaviour
[Tooltip("浸没比例曲线0=刚碰水, 1=充分在水下)。")] public AnimationCurve submergenceCurve = AnimationCurve.Linear(0, 0, 1, 1);
[Header("Damping")] [Tooltip("上浮方向速度阻尼(越大越不弹)。")]
public float verticalDamping = 3.0f;
[Header("Damping")] [Tooltip("上浮方向速度阻尼(越大越不弹)。本版本只在“浮力中心”施加一次不再在每个采样点施加避免90°附近转不动。")]
public float verticalDamping = 0.6f;
[Tooltip("整体角速度阻尼(只施加一次,不要太大)。")] public float angularDamping = 0.6f;
@@ -31,8 +31,15 @@ public class CapsuleBuoyancyStable : MonoBehaviour
[Header("Water Drag")] public float extraDragInWater = 0.8f;
public float extraAngularDragInWater = 0.8f;
#region Crest5相关信息
[Header("Anti-stiction near upright")]
[Tooltip("在接近竖直(例如90->80度附近)时降低vertical damping避免“粘住”。0=关闭。")]
[Range(0f, 1f)]
public float nearUprightDampingReduce = 0.6f;
[Tooltip("接近竖直的判定角度(度)。例如 12 表示在 |angle| < 12° 附近逐步降低阻尼。")] [Range(1f, 30f)]
public float nearUprightAngleDeg = 12f;
#region Crest5相关信息
public WaterRenderer _waterRenderer;
@@ -44,11 +51,7 @@ public class CapsuleBuoyancyStable : MonoBehaviour
readonly SampleFlowHelper _SampleFlowHelper = new();
/// <summary>
/// 查询水面信息点位
/// </summary>
Vector3[] _QueryPoints;
Vector3[] _QueryResultDisplacements;
Vector3[] _QueryResultVelocities;
Vector3[] _QueryResultNormal;
@@ -61,106 +64,147 @@ public class CapsuleBuoyancyStable : MonoBehaviour
CapsuleCollider _cap;
float _baseDrag, _baseAngularDrag;
[SerializeField] private bool _init = false;
void Awake()
{
_rb = GetComponent<Rigidbody>();
_cap = GetComponent<CapsuleCollider>();
_baseDrag = _rb.linearDamping;
_baseAngularDrag = _rb.angularDamping;
}
private void Start()
void Start()
{
int length = Mathf.Max(3, samplePoints);
_QueryPoints = new Vector3[length];
_QueryResultDisplacements = new Vector3[length];
_QueryResultVelocities = new Vector3[length];
_QueryResultNormal = new Vector3[length];
}
public void InitBobber()
{
if (_waterRenderer == null && SceneSettings.Instance)
{
_waterRenderer = SceneSettings.Instance.Water;
}
_cap = GetComponentInChildren<CapsuleCollider>();
_init = true;
}
void FixedUpdate()
{
if (!_waterRenderer)
{
return;
}
if (!_init) return;
if (!_waterRenderer) return;
GetWorldCapsule(out Vector3 a, out Vector3 b, out float radius);
int n = Mathf.Max(3, samplePoints);
if (_QueryPoints == null || _QueryPoints.Length != n)
{
_QueryPoints = new Vector3[n];
_QueryResultDisplacements = new Vector3[n];
_QueryResultVelocities = new Vector3[n];
_QueryResultNormal = new Vector3[n];
}
float fullBuoyancy = _rb.mass * Physics.gravity.magnitude * buoyancyScale;
float perPointMax = fullBuoyancy / n;
float subSum = 0f;
int wetCount = 0;
for (int i = 0; i < _QueryPoints.Length; i++)
// 采样点
for (int i = 0; i < n; i++)
{
float t = (float)i / (n - 1);
Vector3 p = Vector3.Lerp(a, b, t);
_QueryPoints[i] = p;
_QueryPoints[i] = Vector3.Lerp(a, b, t);
}
// 查询
// Crest 查询
var collisions = _waterRenderer.AnimatedWavesLod.Provider;
collisions.Query(GetHashCode(), _ObjectWidth, _QueryPoints, _QueryResultDisplacements,
_QueryResultNormal, _QueryResultVelocities, _Layer);
float subSum = 0f;
int wetCount = 0;
// 用于计算“浮力中心”(Center of Buoyancy)与水流速度平均
Vector3 cobSum = Vector3.zero;
Vector3 wvSum = Vector3.zero;
float cobW = 0f;
// 1) 多点只加浮力不再在每点加vertical damping
for (int i = 0; i < n; i++)
{
float t = (float)i / (n - 1);
Vector3 p = Vector3.Lerp(a, b, t);
Vector3 p = _QueryPoints[i];
float waterH =
_QueryResultDisplacements[i].y + _waterRenderer.SeaLevel; //GaiaConstants.Water.GetWaterHeight(p);
float depth = waterH - p.y; // >0 在水下
float waterH = _QueryResultDisplacements[i].y + _waterRenderer.SeaLevel;
float depth = waterH - p.y;
float sub = Mathf.InverseLerp(-radius, radius, depth); // 0..1
float sub = Mathf.InverseLerp(-radius, radius, depth);
if (sub <= 0f) continue;
sub = Mathf.Clamp01(submergenceCurve.Evaluate(sub));
subSum += sub;
wetCount++;
Vector3 buoyDir = Vector3.up;
cobSum += p * sub;
wvSum += _QueryResultVelocities[i] * sub;
cobW += sub;
// Vector3 waterVel = GaiaConstants.Water.GetWaterVelocity(p);
Vector3 waterVel = _QueryResultVelocities[i];
Vector3 pointVel = _rb.GetPointVelocity(p);
Vector3 relVel = pointVel - waterVel;
// 浮力
Vector3 buoyForce = buoyDir * (perPointMax * sub);
// 只阻尼上浮方向速度分量(防弹跳)
float vUp = Vector3.Dot(relVel, buoyDir);
Vector3 dampForce = -buoyDir * (vUp * verticalDamping * _rb.mass * sub);
_rb.AddForceAtPosition(buoyForce + dampForce, p, ForceMode.Force);
Vector3 buoyForce = Vector3.up * (perPointMax * sub);
_rb.AddForceAtPosition(buoyForce, p, ForceMode.Force);
if (drawDebug)
{
Debug.DrawLine(p, p + buoyForce / (_rb.mass * 10f), Color.cyan, 0f, false);
Debug.DrawLine(p, p + dampForce / (_rb.mass * 10f), Color.yellow, 0f, false);
}
}
float subAvg = (wetCount > 0) ? (subSum / wetCount) : 0f;
// 角阻尼:只加一次(关键修复点
// 2) vertical damping只在“浮力中心”施加一次关键修复不再产生抑制旋转的力矩
if (subAvg > 0f && cobW > 1e-6f)
{
Vector3 cob = cobSum / cobW;
Vector3 waterVelAvg = wvSum / cobW;
// 接近竖直时降低vertical damping避免90->80度“粘住”
float vdScale = 1f;
if (nearUprightDampingReduce > 0f)
{
Vector3 axisWorld = GetAxisWorld(uprightAxis);
float angleFromUp = Vector3.Angle(axisWorld, Vector3.up); // 0=竖直
float t = Mathf.Clamp01(angleFromUp / Mathf.Max(0.001f, nearUprightAngleDeg));
// t=0(很竖直) -> 1(离开竖直)
vdScale = Mathf.Lerp(1f - nearUprightDampingReduce, 1f, t);
}
Vector3 pointVel = _rb.GetPointVelocity(cob);
Vector3 relVel = pointVel - waterVelAvg;
float vUp = Vector3.Dot(relVel, Vector3.up);
Vector3 dampForce = -Vector3.up * (vUp * verticalDamping * _rb.mass * subAvg * vdScale);
_rb.AddForceAtPosition(dampForce, cob, ForceMode.Force);
if (drawDebug)
{
Debug.DrawLine(cob, cob + dampForce / (_rb.mass * 10f), Color.yellow, 0f, false);
}
}
// 3) 角阻尼:只加一次
if (subAvg > 0f)
{
_rb.AddTorque(-_rb.angularVelocity * (angularDamping * _rb.mass * subAvg), ForceMode.Force);
}
// 可选:upright 稳定器(更像“浮漂自动立起来”
// 4) upright(保持你原逻辑
if (subAvg > 0f && uprightSpring > 0f)
{
Vector3 axisWorld = GetAxisWorld(uprightAxis);
Vector3 targetUp = Vector3.up;
// 误差轴axisWorld 需要对齐 targetUp也可反过来按你浮漂模型选
Vector3 errorAxis = Vector3.Cross(axisWorld, targetUp);
float errorMag = errorAxis.magnitude;
@@ -168,7 +212,6 @@ public class CapsuleBuoyancyStable : MonoBehaviour
{
errorAxis /= errorMag;
// “弹簧”力矩 + 阻尼(防止在两个角度间抽动)
Vector3 springTorque = errorAxis * (uprightSpring * errorMag * _rb.mass);
Vector3 dampTorque = -_rb.angularVelocity * (uprightDamping * _rb.mass);
@@ -176,7 +219,7 @@ public class CapsuleBuoyancyStable : MonoBehaviour
}
}
// 入水整体 drag
// 5) 入水 drag
if (subAvg > 0.001f)
{
_rb.linearDamping = _baseDrag + extraDragInWater * subAvg;
@@ -202,7 +245,7 @@ public class CapsuleBuoyancyStable : MonoBehaviour
void GetWorldCapsule(out Vector3 a, out Vector3 b, out float radius)
{
Vector3 lossy = transform.lossyScale;
int dir = _cap.direction; // 0=X,1=Y,2=Z
int dir = _cap.direction;
float scaleAlong = (dir == 0) ? Mathf.Abs(lossy.x) : (dir == 1) ? Mathf.Abs(lossy.y) : Mathf.Abs(lossy.z);
@@ -224,16 +267,14 @@ public class CapsuleBuoyancyStable : MonoBehaviour
b = center + half;
}
private void OnDrawGizmosSelected()
void OnDrawGizmosSelected()
{
if (drawDebug)
{
// 绘制 Rigidbody 的重心点位
Vector3 centerOfMassWorld = transform.TransformPoint(_rb != null ? _rb.centerOfMass : Vector3.zero);
Gizmos.color = Color.cyan;
Gizmos.DrawSphere(centerOfMassWorld, 0.1f);
Gizmos.DrawLine(centerOfMassWorld, centerOfMassWorld + Vector3.up * 0.5f);
}
if (!drawDebug) return;
if (_rb == null) _rb = GetComponent<Rigidbody>();
Vector3 centerOfMassWorld = transform.TransformPoint(_rb != null ? _rb.centerOfMass : Vector3.zero);
Gizmos.color = Color.cyan;
Gizmos.DrawSphere(centerOfMassWorld, 0.1f);
Gizmos.DrawLine(centerOfMassWorld, centerOfMassWorld + Vector3.up * 0.5f);
}
}