45 lines
974 B
C#
45 lines
974 B
C#
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
|
||
|
|
public class AimArrowController : MonoBehaviour
|
||
|
|
{
|
||
|
|
public Image arrow;
|
||
|
|
public float m_AngleSpeed = 1.5f;
|
||
|
|
|
||
|
|
Vector2 startPos;
|
||
|
|
bool isAiming;
|
||
|
|
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
arrow.enabled = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void OnAimStart(Vector2 screenPos)
|
||
|
|
{
|
||
|
|
isAiming = true;
|
||
|
|
arrow.enabled = true;
|
||
|
|
UpdateArrow(screenPos);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void OnAimDrag(Vector2 screenPos)
|
||
|
|
{
|
||
|
|
if (!isAiming) return;
|
||
|
|
UpdateArrow(screenPos);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void OnAimEnd()
|
||
|
|
{
|
||
|
|
arrow.enabled = false;
|
||
|
|
isAiming = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
void UpdateArrow(Vector2 currentPos)
|
||
|
|
{
|
||
|
|
Vector2 dir = currentPos - startPos;
|
||
|
|
if (dir.sqrMagnitude < 0.001f) return;
|
||
|
|
|
||
|
|
float angle = (Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 45f) * m_AngleSpeed;
|
||
|
|
angle = Mathf.Clamp(angle, -60f, 60f);
|
||
|
|
arrow.rectTransform.rotation = Quaternion.Euler(0, 0, angle);
|
||
|
|
}
|
||
|
|
}
|