61 lines
2.0 KiB
C#
61 lines
2.0 KiB
C#
using UnityEngine;
|
||
|
||
/// <summary>
|
||
/// 挂在一块"透明可点击区域"物体上,标记它对应哪条轨道(0-4)。
|
||
/// 该物体需带一个 Collider2D(推荐 BoxCollider2D,可旋转)。
|
||
///
|
||
/// 两种跟随晃动的方式:
|
||
/// A) 直接把本物体作为会晃动的轨道 GameObject 的子物体 —— Unity 自动跟随平移+旋转,
|
||
/// 不需要 followTarget,留空即可。
|
||
/// B) 无法作为子物体时,把会晃动的轨道 Transform 拖到 followTarget,
|
||
/// 本脚本会在 LateUpdate 中把自身对齐到它(位置+旋转),保证点击区跟着晃。
|
||
///
|
||
/// 判定完全不经过这里:命中检测由 TrackTouchInput 遍历触摸完成,
|
||
/// 只调用 InputManager.PressTrack/ReleaseTrack,判定时机仍走 dspTime。
|
||
/// </summary>
|
||
[RequireComponent(typeof(Collider2D))]
|
||
public class TrackTouchZone : MonoBehaviour
|
||
{
|
||
[Tooltip("本区域控制的轨道:0=红 1=绿 2=黄 3=紫 4=蓝")]
|
||
[Range(0, 4)]
|
||
public int trackIndex = 0;
|
||
|
||
[Tooltip("可选。若本物体不是轨道的子物体,把会晃动的轨道 Transform 拖到这里,区域会每帧对齐到它(位置+旋转)。")]
|
||
public Transform followTarget;
|
||
|
||
[Tooltip("followTarget 生效时,是否同步旋转(轨道会旋转时勾上)。")]
|
||
public bool followRotation = true;
|
||
|
||
private Collider2D _collider;
|
||
|
||
public Collider2D Collider
|
||
{
|
||
get
|
||
{
|
||
if (_collider == null) _collider = GetComponent<Collider2D>();
|
||
return _collider;
|
||
}
|
||
}
|
||
|
||
private void Awake()
|
||
{
|
||
_collider = GetComponent<Collider2D>();
|
||
}
|
||
|
||
private void LateUpdate()
|
||
{
|
||
// 在所有晃动逻辑(Update)之后对齐,避免落后一帧。
|
||
if (followTarget == null) return;
|
||
|
||
transform.position = new Vector3(
|
||
followTarget.position.x,
|
||
followTarget.position.y,
|
||
transform.position.z);
|
||
|
||
if (followRotation)
|
||
{
|
||
transform.rotation = followTarget.rotation;
|
||
}
|
||
}
|
||
}
|