TigerProject/Assets/Scripts/Player/PlayerAttack.cs
qoralstmd6825 e19fd24f3f Player Attack Added
플레이어 기본 공격과 대쉬공격 판정 밑 이동 구현했습니다.
2026-01-21 21:49:50 +09:00

112 lines
3.6 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerAttack : MonoBehaviour
{
[SerializeField] private Vector2 normalAttackBoxSize = new Vector2(2, 1);
[SerializeField] private Vector2 dashAttackBoxSize = new Vector2(2, 1);
[SerializeField] private float dashAttackDistance = 5f;
[SerializeField] private float attackOffset = 1.0f;
[SerializeField] private LayerMask enemyLayer;
[SerializeField] private float dashSpeed = 15f;
private float normalAttackAngle = 0f;
private Vector2 dashAttackDirection = Vector2.zero;
private Vector2 attackOrigin;
public bool isDashing { get; private set; } = false;
private Camera cam;
private Rigidbody2D rb;
private void Start()
{
cam = Camera.main;
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
if(!isDashing)
SetAttackAngle();
}
private void OnAttack(InputValue value)
{
if (!value.isPressed || isDashing) return;
if (Mouse.current.leftButton.isPressed)
{
Debug.Log("일반공격");
NormalAttack();
}
if (Mouse.current.rightButton.isPressed)
{
Debug.Log("대쉬공격");
StartCoroutine(DashAttackRoutine());
}
}
private void SetAttackAngle()
{
Vector2 mousePositionVector = cam.ScreenToWorldPoint(Mouse.current.position.value);
Vector2 direction = (mousePositionVector - (Vector2)transform.position).normalized;
normalAttackAngle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
dashAttackDirection = direction;
attackOrigin = (Vector2)transform.position + (direction * attackOffset);
}
private void NormalAttack()
{
Collider2D[] enemies = Physics2D.OverlapBoxAll(attackOrigin, normalAttackBoxSize, normalAttackAngle, enemyLayer);
foreach (Collider2D target in enemies)
{
Debug.Log("일반공격 적중: " + target.name);
}
}
private IEnumerator DashAttackRoutine()
{
isDashing = true;
rb.linearVelocity = Vector2.zero;
float dashDuration = dashAttackDistance / dashSpeed;
float elapsedTime = 0f;
rb.linearVelocity = dashAttackDirection * dashSpeed;
Debug.Log(dashAttackDirection);
float originGravity = rb.gravityScale;
rb.gravityScale = 0;
while (elapsedTime < dashDuration)
{
Vector2 currentOrigin = (Vector2)transform.position + (dashAttackDirection * attackOffset);
RaycastHit2D[] enemies = Physics2D.BoxCastAll(currentOrigin, dashAttackBoxSize, normalAttackAngle, dashAttackDirection, 0f, enemyLayer);
foreach (RaycastHit2D target in enemies)
{
Debug.Log("대쉬 공격 적중: " + target.collider.name);
Destroy(target.collider.gameObject);
}
elapsedTime += Time.deltaTime;
yield return null;
}
rb.linearVelocity = Vector2.zero;
isDashing = false;
rb.gravityScale = originGravity;
}
private void OnDrawGizmos()
{
Vector2 drawStartPos = Application.isPlaying ? attackOrigin : (Vector2)transform.position + (Vector2.right * attackOffset);
Gizmos.color = new Color(1, 0, 0, 0.5f);
Gizmos.matrix = Matrix4x4.TRS(drawStartPos, Quaternion.Euler(0, 0, normalAttackAngle), Vector3.one);
Gizmos.DrawCube(Vector3.zero, normalAttackBoxSize);
}
}