This commit is contained in:
2025-05-10 20:57:53 +08:00
parent dea9a4f58e
commit ebcf8918dd
5 changed files with 215 additions and 194 deletions

View File

@@ -934,13 +934,13 @@ MonoBehaviour:
m_EditorClassIdentifier:
mouseOffset: 10
nodeDistance: 0.1
nodeColliderRadius: 0.3
nodeColliderRadius: 0.2
gravityStrength: 1.962
totalNodes: 500
totalLength: 10
velocityDampen: 0.95
stiffness: 0.99
iterateCollisionsEvery: 2
iterations: 20
iterateCollisionsEvery: 1
iterations: 10
colliderBufferSize: 1
ropeWidth: 0.02
--- !u!4 &1418847131

View File

@@ -11,7 +11,7 @@ public class Rope : MonoBehaviour
[SerializeField] float nodeColliderRadius = 0.2f;
[SerializeField] float gravityStrength = 2;
[SerializeField, Range(1, 500)] int totalNodes = 100;
[SerializeField] float totalLength = 10f; // 新增总长度参数
[SerializeField, Range(0, 1)] float velocityDampen = 0.95f;
[SerializeField, Range(0, 0.99f)] float stiffness = 0.8f;
[SerializeField, Range(1, 10)] int iterateCollisionsEvery = 1;
@@ -36,9 +36,20 @@ public class Rope : MonoBehaviour
LineRenderer lineRenderer;
GameObject nodeTester;
SphereCollider nodeCollider;
int totalNodes; // 现在由代码计算
void Awake()
{
// 计算节点数量
totalNodes = Mathf.FloorToInt(totalLength / nodeDistance) + 1;
float remainingLength = totalLength % nodeDistance;
// 如果剩余长度大于0增加一个节点
if (remainingLength > 0 && totalLength > nodeDistance)
{
totalNodes++;
}
// 初始化数组
currentNodePositions = new Vector3[totalNodes];
previousNodePositions = new Vector3[totalNodes];
@@ -59,9 +70,12 @@ public class Rope : MonoBehaviour
Vector3 startPos = transform.position;
for (int i = 0; i < totalNodes; i++)
{
// 如果是最后一个节点且有剩余长度,使用剩余长度
float distance = (i == totalNodes - 1 && remainingLength > 0) ? remainingLength : nodeDistance;
currentNodePositions[i] = startPos;
previousNodePositions[i] = startPos;
startPos.y -= nodeDistance;
startPos.y -= distance;
}
// 设置线渲染器
@@ -141,7 +155,6 @@ public class Rope : MonoBehaviour
// 预计算所有常用值
float halfStiffness = 0.5f * stiffness;
float sqrNodeDistance = nodeDistance * nodeDistance;
int nodeCountMinusOne = totalNodes - 1;
for (int i = 0; i < nodeCountMinusOne; i++)
@@ -150,13 +163,19 @@ public class Rope : MonoBehaviour
Vector3 node2 = currentNodePositions[i + 1];
Vector3 diff = node1 - node2;
// 计算期望的距离 - 如果是最后一个段且有剩余长度,使用剩余长度
float desiredDistance = (i == nodeCountMinusOne - 1 && (totalLength % nodeDistance) > 0)
? (totalLength % nodeDistance)
: nodeDistance;
float sqrDesiredDistance = desiredDistance * desiredDistance;
float sqrDistance = diff.x * diff.x + diff.y * diff.y + diff.z * diff.z;
// 只有当距离差异超过一定阈值时才调整
if (Mathf.Abs(sqrDistance - sqrNodeDistance) > 0.001f)
if (Mathf.Abs(sqrDistance - sqrDesiredDistance) > 0.001f)
{
float distance = Mathf.Sqrt(sqrDistance);
float difference = nodeDistance - distance;
float difference = desiredDistance - distance;
Vector3 direction = diff / distance; // 比 normalized 更快
Vector3 adjustment = direction * (difference * halfStiffness);

File diff suppressed because one or more lines are too long

View File

@@ -141,8 +141,7 @@ PlayerSettings:
visionOSBundleVersion: 1.0
tvOSBundleVersion: 1.0
bundleVersion: 0.1.0
preloadedAssets:
- {fileID: -944628639613478452, guid: 052faaac586de48259a63d0c4782560b, type: 3}
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0
m_HolographicPauseOnTrackingLoss: 1

View File

@@ -18,6 +18,9 @@ EditorUserSettings:
value: 5505015f5c515a085f5b092149760f441716407a787d7564287b1b36e7e1366e
flags: 0
RecentlyUsedSceneGuid-3:
value: 5705040755540d0b5c5f547b417707134216487d7b707e6529714c6be6e33169
flags: 0
RecentlyUsedSceneGuid-4:
value: 5a5757560101590a5d0c0e24427b5d44434e4c7a7b7a23677f2b4565b7b5353a
flags: 0
vcSharedLogLevel: