38 lines
1.0 KiB
C#
38 lines
1.0 KiB
C#
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
public class AimController : MonoBehaviour
|
||
|
|
{
|
||
|
|
public LineRenderer line;
|
||
|
|
public float maxDistance = 5f;
|
||
|
|
|
||
|
|
private Vector2 startPos;
|
||
|
|
private bool isAiming;
|
||
|
|
|
||
|
|
void Update()
|
||
|
|
{
|
||
|
|
if (Input.GetMouseButtonDown(0))
|
||
|
|
{
|
||
|
|
startPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||
|
|
isAiming = true;
|
||
|
|
line.enabled = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Input.GetMouseButton(0) && isAiming)
|
||
|
|
{
|
||
|
|
Vector2 currentPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||
|
|
Vector2 dir = Vector2.ClampMagnitude(currentPos - startPos, maxDistance);
|
||
|
|
|
||
|
|
line.SetPosition(0, transform.position);
|
||
|
|
line.SetPosition(1, (Vector2)transform.position + dir);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Input.GetMouseButtonUp(0) && isAiming)
|
||
|
|
{
|
||
|
|
Vector2 endPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||
|
|
Vector2 shootDir = (endPos - startPos).normalized;
|
||
|
|
|
||
|
|
line.enabled = false;
|
||
|
|
isAiming = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|