summaryrefslogtreecommitdiff
path: root/Assets/Scripts
diff options
context:
space:
mode:
authorSmallFox <2806143047@qq.com>2026-01-31 00:59:22 +0800
committerSmallFox <2806143047@qq.com>2026-01-31 00:59:22 +0800
commit59be0cfc9ebd69b1e0787dd3fb4c3b39e46b90db (patch)
treea5879319f09c44cd43f7b7c0431bc0f8540e2c1a /Assets/Scripts
parent28fbde36e76429bfb2bf577d9e3da77db745ef3d (diff)
parent81cc6cf8bbc2572528252b6ed67f90aa179c6542 (diff)
Merge branch 'main' of catilgrass.cn:GameJamTemplate into dev_lys
Diffstat (limited to 'Assets/Scripts')
-rw-r--r--Assets/Scripts/DragItem.cs79
-rw-r--r--Assets/Scripts/DragItem.cs.meta2
2 files changed, 81 insertions, 0 deletions
diff --git a/Assets/Scripts/DragItem.cs b/Assets/Scripts/DragItem.cs
new file mode 100644
index 0000000..46ba50f
--- /dev/null
+++ b/Assets/Scripts/DragItem.cs
@@ -0,0 +1,79 @@
+using UnityEngine;
+
+public class DragItem : MonoBehaviour
+{
+ private Vector3 SelfPivot
+ {
+ get
+ {
+ var p = transform.position;
+ return new Vector3(p.x, 0, p.z);
+ }
+ }
+
+ private Vector3 DraggerPivot
+ {
+ get
+ {
+ var p = dragger.position;
+ return new Vector3(p.x, 0, p.z);
+ }
+ }
+
+ private bool _dragging;
+ public Transform dragger;
+
+ private Vector3 _lastDirection;
+ private Vector3 _lastSelfPivot;
+ private float _lastAngle;
+ private float _lastLength;
+ private int _currentStep;
+
+ private void FixedUpdate()
+ {
+ FixedUpdateDragger();
+
+ // 判断是否在拖拽
+ if (! _dragging) return;
+
+ // 位置计算
+ var draggerPivot = DraggerPivot;
+ var selfPivot = SelfPivot;
+ var direction = (_lastSelfPivot - draggerPivot).normalized;
+ var position = draggerPivot + direction * _lastLength;
+ var y = transform.position.y;
+ transform.position = new Vector3(
+ position.x,
+ y,
+ position.z
+ );
+
+ // 角度计算
+ if (direction != _lastDirection)
+ {
+ var angle = Vector3.SignedAngle(direction, _lastDirection, Vector3.up);
+ var selfAngle = transform.eulerAngles;
+ selfAngle.y -= angle;
+ transform.eulerAngles = selfAngle;
+ }
+
+ // 记录值
+ _lastSelfPivot = selfPivot;
+ _lastDirection = direction;
+ }
+
+ private void FixedUpdateDragger()
+ {
+ if (!_dragging && dragger != null)
+ {
+ _dragging = true;
+ var draggerPivot = DraggerPivot;
+ _lastSelfPivot = SelfPivot;
+ _lastLength = Vector3.Distance(draggerPivot, SelfPivot);
+ _lastDirection = (_lastSelfPivot - draggerPivot).normalized;
+ }
+
+ if (_dragging && dragger == null)
+ _dragging = false;
+ }
+}
diff --git a/Assets/Scripts/DragItem.cs.meta b/Assets/Scripts/DragItem.cs.meta
new file mode 100644
index 0000000..d32339a
--- /dev/null
+++ b/Assets/Scripts/DragItem.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: c69e2aa4d45550a818a819efbd91c846 \ No newline at end of file