화면을 클릭하면 오브젝트가 방사형으로 확산되는 걸 만들어봅니다. 오브젝트 개수와 퍼지는 속도도 조절 가능.
[RadialWaveControl.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RadialWaveControl : MonoBehaviour
{
[SerializeField] int numWave;
[SerializeField] GameObject waveImg;
[SerializeField] float radius = 5f, speed = 5f;
Vector2 startSpot;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
startSpot = Camera.main.ScreenToWorldPoint(Input.mousePosition);
SpawnWave(numWave);
}
}
void SpawnWave(int numWave)
{
float angleStep = 360f / numWave;
float angle = 0f;
for(int i = 0; i <= numWave - 1; i++)
{
float waveXpos = startSpot.x + Mathf.Sin((angle * Mathf.PI) / 180) * radius;
float waveYpos = startSpot.y + Mathf.Cos((angle * Mathf.PI) / 180) * radius;
Vector2 waveVector = new Vector2(waveXpos, waveYpos);
Vector2 waveMoveDir = (waveVector - startSpot).normalized * speed;
var wave = Instantiate(waveImg, startSpot, Quaternion.identity);
wave.GetComponent<Rigidbody2D>().velocity = new Vector2(waveMoveDir.x, waveMoveDir.y);
angle += angleStep;
}
}
}
댓글