60 lines
1.3 KiB
C#
60 lines
1.3 KiB
C#
using UnityEngine;
|
|
|
|
public class Projectile : MonoBehaviour
|
|
{
|
|
public float speed = 12f;
|
|
public int maxBounce = 3;
|
|
public float radius = 0.1f; // 충돌 보정용
|
|
|
|
Vector2 dir;
|
|
int bounceCount;
|
|
|
|
public void Set()
|
|
{
|
|
dir = transform.up.normalized;
|
|
bounceCount = maxBounce;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
float moveDist = speed * Time.deltaTime;
|
|
|
|
// 🔍 이동 방향으로 Raycast
|
|
RaycastHit2D hit = Physics2D.CircleCast(
|
|
transform.position,
|
|
radius,
|
|
dir,
|
|
moveDist
|
|
);
|
|
|
|
if (hit.collider != null)
|
|
{
|
|
// 충돌 지점으로 이동
|
|
transform.position = hit.point + hit.normal * radius;
|
|
|
|
Bounce(hit.normal);
|
|
}
|
|
else
|
|
{
|
|
transform.position += (Vector3)(dir * moveDist);
|
|
}
|
|
}
|
|
|
|
void Bounce(Vector2 normal)
|
|
{
|
|
bounceCount--;
|
|
|
|
if (bounceCount < 0)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
// 🔥 반사 방향 계산
|
|
dir = Vector2.Reflect(dir, normal).normalized;
|
|
|
|
// 반사 후 방향에 맞게 회전
|
|
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90f;
|
|
transform.rotation = Quaternion.Euler(0, 0, angle);
|
|
}
|
|
} |