OneShotOneKill/Assets/Script/InGame/Actor/AimArrowController.cs

53 lines
1.3 KiB
C#

using UnityEngine;
using UnityEngine.UI;
public class AimArrowController : MonoBehaviour
{
public Image arrow;
public float m_AngleSpeed = 1.5f;
public GameObject projectilePrefab;
public Transform projectileParent;
Vector2 startPos;
bool isAiming;
void Start()
{
Application.targetFrameRate = 60;
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;
var proj = DSUtil.Get_Clone<Projectile>(projectilePrefab, projectileParent);
proj.transform.position = arrow.transform.position;
proj.transform.rotation = arrow.transform.rotation;
proj.Set();
}
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);
}
}