blob: 8e7a69d0f485957279d74dcef241b27eda1cdd29 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
using System;
using UnityEngine;
/// <summary>
/// 简化版玩家拖拽脚本(Trigger触发判定,仅保留核心拖拽逻辑)
/// </summary>
[RequireComponent(typeof(PlayerControl))]
public class PlayerDrag : MonoBehaviour
{
private DragItem _currentDragItem;
private bool _isDragging;
private bool _lastFrameGrabbing;
private PlayerControl _control;
private void Awake()
{
_control = GetComponent<PlayerControl>();
}
private void Update()
{
var grabbing = _control.grabbing;
var keyDown = !_lastFrameGrabbing && grabbing;
var keyUp = _lastFrameGrabbing && !grabbing;
// 按下G键:抓取物品(仅当触发接触且未拖拽时生效)
if (keyDown && !_isDragging && _currentDragItem != null)
{
_isDragging = true;
_currentDragItem.dragger = transform; // 给物品赋值拖拽锚点(玩家)
}
// 松开G键:放下物品(解除关联)
if (keyUp && _isDragging)
{
_isDragging = false;
if (_currentDragItem != null)
{
_currentDragItem.dragger = null; // 清空物品的拖拽锚点
}
_currentDragItem = null;
}
// 更新抓取
_lastFrameGrabbing = grabbing;
}
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;
}
}
}
|