본문 바로가기
Dev/Unity(C#)

[Unity3D] 플레이어의 주위를 도는 파티클

by E.Clone 2020. 7. 13.



이펙트를 위한 파티클시스템을 만들어 Effect01.cs 스크립트를 사용한다.

Player Center에는 플레이어의 중심을 알 수 있는 오브젝트를 사용한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Effect01.cs
using UnityEngine;
 
public class Effect01 : MonoBehaviour
{
    public GameObject PlayerCenter;
    private Vector3 PlayerCenterPos;
    private Vector3 Offset;
    public float Radius; // 회전 반지름
    public float Speed; // 회전 각속도
    public float OffsetDelay; // 각도 초기값
    public float OffsetLerp; // PlayerCenter에 따라붙는 속도
    private Vector3 ToPos; // PlayerCenterPos + Offset
    
    void Update()
    {
        PlayerCenterPos = PlayerCenter.transform.position;
 
        // 움직이는 모양을 설정
        Offset = new Vector3(
            Mathf.Cos(Time.timeSinceLevelLoad * Speed + OffsetDelay*(float)Mathf.PI*2 ),
            0,
            Mathf.Sin(Time.timeSinceLevelLoad * Speed + OffsetDelay * (float)Mathf.PI * 2));
 
        // 기본 세팅
        Offset *= Radius;
 
        // 좀 더 랜덤한 움직임을 위한 식
        //Offset *= Mathf.Abs(2.0f- (float)(Time.timeSinceLevelLoad % 4.0)) * 0.5f * Radius + Radius;
 
        ToPos = PlayerCenterPos + Offset;
 
        // Lerp함수는 출발지에서 도착지까지의 자연스러운 움직임이 가능토록 함
        transform.position = Vector3.Lerp(transform.position, ToPos, OffsetLerp);
    }
}
 
cs


반응형