diff options
| author | SmallFox <2806143047@qq.com> | 2026-01-31 12:31:28 +0800 |
|---|---|---|
| committer | SmallFox <2806143047@qq.com> | 2026-01-31 12:31:28 +0800 |
| commit | 64e326ff1720ceb5e4bdcb9a763f62de8ffbee4f (patch) | |
| tree | 7d2b321fb203b69e928641e5e1c49512bfcb694e /Assets/Scripts | |
| parent | 343a1aa731a85a8a866d0466d29931e5a9c8dfb0 (diff) | |
Drag
Diffstat (limited to 'Assets/Scripts')
| -rw-r--r-- | Assets/Scripts/GrabStateMachine.cs | 47 | ||||
| -rw-r--r-- | Assets/Scripts/GrabStateMachine.cs.meta | 2 | ||||
| -rw-r--r-- | Assets/Scripts/PlayerDrag.cs | 70 | ||||
| -rw-r--r-- | Assets/Scripts/PlayerDrag.cs.meta | 2 |
4 files changed, 121 insertions, 0 deletions
diff --git a/Assets/Scripts/GrabStateMachine.cs b/Assets/Scripts/GrabStateMachine.cs new file mode 100644 index 0000000..8242097 --- /dev/null +++ b/Assets/Scripts/GrabStateMachine.cs @@ -0,0 +1,47 @@ +using UnityEngine; + +public class GrabStateMachine : MonoBehaviour +{ + private bool grabbing = false; + + // 动画组件引用(需要在Inspector面板赋值,或通过代码自动获取) + [Header("动画组件引用")] + [SerializeField] private Animator anim; + + // 按键设置(可在Inspector面板修改,无需硬编码) + [Header("控制按键")] + [SerializeField] private KeyCode grabKey = KeyCode.G; + + /// <summary> + /// 初始化 + /// </summary> + private void Start() + { + anim = GetComponent<Animator>(); + } + + private void Update() + { + DetectGrabKeyInput(); + UpdateAnimatorState(); + } + + private void DetectGrabKeyInput() + { + if (Input.GetKey(grabKey)) + { + grabbing = true; + } + + if (Input.GetKeyUp(grabKey)) + { + grabbing = false; + } + } + + + private void UpdateAnimatorState() + { + anim.SetBool("grabbing", grabbing); + } +} diff --git a/Assets/Scripts/GrabStateMachine.cs.meta b/Assets/Scripts/GrabStateMachine.cs.meta new file mode 100644 index 0000000..70b2bc3 --- /dev/null +++ b/Assets/Scripts/GrabStateMachine.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 811939d88c0df0c44bf13cae7f13a581
\ No newline at end of file diff --git a/Assets/Scripts/PlayerDrag.cs b/Assets/Scripts/PlayerDrag.cs new file mode 100644 index 0000000..10300af --- /dev/null +++ b/Assets/Scripts/PlayerDrag.cs @@ -0,0 +1,70 @@ +using UnityEngine; + +/// <summary> +/// 简化版玩家拖拽脚本(Trigger触发判定,仅保留核心拖拽逻辑) +/// </summary> +public class PlayerDrag : MonoBehaviour +{ + // 拖拽核心配置 + [SerializeField] private KeyCode grabKey = KeyCode.G; // 抓取按键 + + // 临时缓存:当前触发接触的可拖拽物品 + private DragItem currentDragItem; + // 标记是否处于拖拽状态 + private bool isDragging; + + private void Update() + { + // 仅检测G键输入,控制抓取/松开(核心逻辑不变) + CheckGrabInput(); + } + + + private void CheckGrabInput() + { + // 按下G键:抓取物品(仅当触发接触且未拖拽时生效) + if (Input.GetKeyDown(grabKey) && !isDragging && currentDragItem != null) + { + isDragging = true; + currentDragItem.dragger = this.transform; // 给物品赋值拖拽锚点(玩家) + } + + // 松开G键:放下物品(解除关联) + if (Input.GetKeyUp(grabKey) && isDragging) + { + isDragging = false; + if (currentDragItem != null) + { + currentDragItem.dragger = null; // 清空物品的拖拽锚点 + } + currentDragItem = null; + } + } + + private void OnTriggerStay(Collider other) + { + // 如果已在拖拽状态,直接返回,不处理新物品 + if (isDragging) return; + + // 尝试获取对方的DragItem组件,缓存为当前可抓取物品 + DragItem dragItem = other.GetComponent<DragItem>(); + if (dragItem != null) + { + currentDragItem = dragItem; + } + } + + + private void OnTriggerExit(Collider other) + { + // 如果已在拖拽状态,直接返回(避免拖拽中丢失目标) + if (isDragging) return; + + // 确认离开的是当前缓存的物品,清空缓存 + DragItem dragItem = other.GetComponent<DragItem>(); + if (dragItem != null && dragItem == currentDragItem) + { + currentDragItem = null; + } + } +}
\ No newline at end of file diff --git a/Assets/Scripts/PlayerDrag.cs.meta b/Assets/Scripts/PlayerDrag.cs.meta new file mode 100644 index 0000000..5512a1e --- /dev/null +++ b/Assets/Scripts/PlayerDrag.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 8cbfd87026c9dc54bbd983d8aa98eafd
\ No newline at end of file |
