[Unity] Particle System 종료 시 자동으로 Destroy하기

JH Park·2021년 7월 8일
0

Unity

목록 보기
2/3

문제 상황

식물이 다 자라서 작물이 될 때
이런 Particle System이 나타나도록 했다.

게임 중에 작물이 생기기 때문에 Instatinate로 작물과 이 Particle System이 나타나야된다.

GameObject crop = Instantiate(Resources.Load("Prefabs/Crops/Crop_" + num), selectedplant.transform.position+new Vector3(0,0.3f,0),Quaternion.identity)as GameObject;
crop.transform.parent=Crops.transform.GetChild(num).transform;
crop.transform.localScale = new Vector3(1, 1, 1);
ParticleSystem cropps = Instantiate(Resources.Load("Prefabs/CropPs"), crop.transform.position, Quaternion.identity) as ParticleSystem;

이렇게만 구현하게되면 작물로 변할 때마다 CropPs가 생성되어서
이렇게 계속 쌓이게 된다.

해결방안

다시는 이용되지 않을 쓰레기값이라서 Particle System이 바로 Destroy시킬 방법을 찾아보았다.

1. Particle System: Stop Action-->Destroy

Particle System 자체적으로 Auto Destory가 있지 않을까 싶어서 자세히 살펴보았다.
Stop Action을 Destory로 바꿔준다.
코드로 작성하지 않아도 Particle System 자체적인 기능으로 해결되서 간단하고 작동도 잘됐다.

2. 코드1

http://developug.blogspot.com/2014/10/auto-destroy-particle-system.html
Update문으로 Particle System이 살아있는지 체크하고 끝났을 경우 이를 Destory한다.

3. 코드2

duration을 이용하여 그 시간만큼 기다렸다가 Destroy 시킨다.

profile
Computer Engineering Student

0개의 댓글