插头修改提交

This commit is contained in:
2025-02-19 17:25:37 +08:00
parent 722a3013ab
commit 430ab63f5c
8 changed files with 292 additions and 49 deletions
@@ -13,10 +13,10 @@ public class PhysicCable : MonoBehaviour
{
physicLine=transform.GetChild(0).GetComponent<PhysicLineSegment>();
}
public void Init()
public void Init(Vector3 endPos)
{
physicLine.Initialize(StartTranform.position,endTransform.position);
physicLine.Initialize(StartTranform.position,endPos);
}
// Update is called once per frame
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
[RequireComponent(typeof(LineRenderer))]
public class PhysicLineSegment : MonoBehaviour
@@ -13,16 +14,24 @@ public class PhysicLineSegment : MonoBehaviour
private List<VerletStick> sticks = new List<VerletStick>();
[HideInInspector]
public LineRenderer lineRenderer;
public int segmentCount;
private int segmentCount;
public float gravity = -9.8f;
private float originalLength;
private float targetLength;
public void Initialize(Vector3 start, Vector3 end)
{
particles.Clear();
sticks.Clear();
if (lineRenderer != null)
{
lineRenderer.positionCount = 0;
}
this.start = start;
this.end = end;
originalLength = Vector3.Distance(start, end);
targetLength = originalLength; // 初始化目标长度
segmentCount = Mathf.RoundToInt(originalLength / segmentSpacing);
lineRenderer = GetComponent<LineRenderer>();
@@ -51,11 +60,10 @@ public class PhysicLineSegment : MonoBehaviour
UpdateLine();
for (int i = 0; i < segmentCount; i++)
{
if(lineRenderer!=null)
if (lineRenderer != null)
{
lineRenderer.SetPosition(i, particles[i].position);
}
}
}
@@ -77,6 +85,51 @@ public class PhysicLineSegment : MonoBehaviour
}
}
}
public void SetTargetLength(float newLength, float duration)
{
DOTween.To(() => targetLength, x => targetLength = x, newLength, duration);
}
public void UpdateLineLength(float newLength)
{
// 计算新的段数
int newSegmentCount = Mathf.RoundToInt(newLength / segmentSpacing);
// 如果新的段数大于当前段数,增加粒子
if (newSegmentCount > segmentCount)
{
for (int i = segmentCount; i < newSegmentCount; i++)
{
var particle = new VerletParticle(Vector3.Lerp(start, end, i / (float)newSegmentCount));
particles.Add(particle);
if (i > 0)
{
sticks.Add(new VerletStick(particles[i - 1], particles[i], segmentSpacing));
}
}
}
// 如果新的段数小于当前段数,减少粒子
else if (newSegmentCount < segmentCount)
{
particles.RemoveRange(newSegmentCount, segmentCount - newSegmentCount);
sticks.RemoveRange(newSegmentCount - 1, segmentCount - newSegmentCount);
}
// 更新段数
segmentCount = newSegmentCount;
if(lineRenderer!=null)
{
lineRenderer.positionCount = segmentCount;
}
// 更新粒子位置
for (int i = 0; i < segmentCount; i++)
{
particles[i].position = Vector3.Lerp(start, end, i / (float)segmentCount);
}
}
public void UpdatePlug(Transform plug)
{
if (plug != null && particles.Count > 1)
@@ -87,14 +140,12 @@ public class PhysicLineSegment : MonoBehaviour
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
plug.rotation = Quaternion.Euler(0, 0, angle - 90f);
plug.position = plugPosition + direction.normalized * (plugLength / 2);
}
}
public void UpdateEndPoint(Transform transform)
{
particles[particles.Count - 1].isLocked = true;
particles[particles.Count - 1].position = transform.position;
}
}
@@ -67,7 +67,7 @@ namespace AibisDream.FixSystem
float t = Mathf.InverseLerp(config.dstMin, config.dstMax, (_start.position - _end.position).magnitude);
float F = Mathf.Lerp(config.forceMin, config.forceMax, t);
_points[0] = _start.position;
_points[^1] = _end.GetChild(0).position;
_points[^1] = _end.position;
for (int ik = 0; ik < config.k; ik++)
{
for (int i = 1; i < _points.Length - 1; i++)
@@ -67,8 +67,9 @@ namespace AibisDream.FixSystem
private void InitSystem()
{
//PlugRef.InitPlug(InitSocket);
PhysicCableRef.Init();
//PhysicCableRef.Init();
PlugRef.InitPlug(InitSocket);
}
public void Update()
{
@@ -81,7 +82,7 @@ namespace AibisDream.FixSystem
if (curSocket == InitSocket) return;
// 不在初始插孔里,就从当前插孔拔出来,插回初始插孔
PlugRef.PullUpSocket();
PlugRef.InsertSocket(InitSocket);
PlugRef.ReturnToStartPosition();
}
/// <summary>
+49 -12
View File
@@ -13,10 +13,12 @@ namespace AibisDream.FixSystem
#region
private Vector3 _startPos;
private Vector3 _startRotate;
private readonly Vector3 _pickupOffset = new(0, 0, 0);
private readonly Quaternion _pickupRotationOffset = Quaternion.Euler(0, 0, 30);
private readonly Quaternion initRotation = Quaternion.Euler(0, 0, 180);
#endregion
#region
@@ -29,11 +31,13 @@ namespace AibisDream.FixSystem
#endregion
private bool _isDragging;
private bool _isPhysicCableActive=false;
private void Awake()
{
InitComponentRef();
EventRegister();
_startPos = transform.position; // 初始化时记录初始位置
}
private void InitComponentRef()
@@ -53,14 +57,9 @@ namespace AibisDream.FixSystem
public void InitPlug(ISocket initSocket)
{
//InsertSocket(initSocket);
ReturnToStartPosition();
}
public void Update()
{
//_cableSystem.PhysicCableRef.physicLine.UpdateEndPoint(this.transform);
_cableSystem.PhysicCableRef.physicLine.UpdatePlug(this.transform);
}
/// <summary>
/// 插入某个socket
@@ -129,11 +128,17 @@ namespace AibisDream.FixSystem
// 开启拖拽
_isDragging = true;
// 进行插拔动作
if (_cableSystem.curSocket != null) PullUpSocket();
// 调整插头形状
AdjustPlugPos();
// 关闭 PhysicCable 的更新,并显示 Cable 的 LineRenderer
_cableSystem.PhysicCableRef.GetComponent<LineRenderer>().enabled = false;
_isPhysicCableActive = false;
_cableSystem.CableRef.GetComponent<LineRenderer>().enabled = true;
}
private void OnPointerUp(BaseEventData eventData)
@@ -149,19 +154,51 @@ namespace AibisDream.FixSystem
}
else
{
// 没找到就插回初始Socket,需要播动画
// transform.DOMove(_cableSystem.InitSocket.GetSocketPos(), 0.1f).OnComplete(() =>
// {
// InsertSocket(_cableSystem.InitSocket);
// });
// 没找到就插回初始位置
ReturnToStartPosition();
}
}
public void ReturnToStartPosition()
{
transform.DOMove(_startPos, 0.1f).OnComplete(() =>
{
SwitchToPhysicCableState();
});
}
private void AdjustPlugPos()
{
transform.position += _pickupOffset;
transform.rotation = _pickupRotationOffset;
}
private void PlugPosBack()
{
transform.position -= _pickupOffset;
transform.rotation = initRotation;
}
private void SwitchToPhysicCableState()
{
// 隐藏 Cable 的 LineRenderer
PlugPosBack();
_cableSystem.PhysicCableRef.Init(transform.position);
_cableSystem.CableRef.GetComponent<LineRenderer>().enabled = false;
// 激活 PhysicCable
_cableSystem.PhysicCableRef.GetComponent<LineRenderer>().enabled = true;
_isPhysicCableActive = true;
}
private void Update()
{
if (_isPhysicCableActive)
{
_cableSystem.PhysicCableRef.physicLine.UpdatePlug(this.transform);
}
}
void OnDrawGizmosSelected()
{