Unity/Unity 기초

Unity 강의 5일차 (4) - 파티클 및 음향 효과

하참이 2025. 1. 20. 22:44

본 강의는 다음 문서를 참고하여 제작하였습니다. 자세한 내용은 하단 링크를 참조하시거나 댓글로 질문 남겨주시면 성심성의껏 답변 드리겠습니다.

 

https://learn.unity.com/tutorial/3-4gang-patikeul-mic-eumhyang-hyogwa?uv=2021.3&pathwayId=63ca4663edbc2a7be103183f&missionId=63c7676bedbc2a66daff99b1&projectId=63ca1d01edbc2a5f4807369e#63ca1d74edbc2a629048f0d1

 

3.4강 - 파티클 및 음향 효과 - Unity Learn

개요: 이제 게임의 그래픽이 아주 근사하게 완성되었는데, 중요한 몇 가지가 빠져 있습니다. 바로 음향 효과와 파티클 효과입니다. 음향과 음악은 조용한 게임 월드에 생동감을 더하고, 파티클

learn.unity.com

 


 

 

파티클은 입자를 의미하죠. Unity에서는 이펙트 효과를 생성하기 위해 파티클을 사용합니다.

 

Course Library > Particles에서 FX_Explosion_Smoke를 하이어라키창으로 드래그해서 이펙트를 확인합니다.

 

 

 

 

펑 터지는 효과가 게임의 생동감을 불어줄 것 같네요!

 

 

작게 써져있어서 잘 안보이고 이미 해제 되어있지만 Play On Awake는 해제되어야합니다. 파티클을 Player로 드래그 해 자식 오브젝트로 만듭시다.

 

 

 

 

파티클을 선택한 상태에서 씬 뷰 우측 하단에 나오는 창을 이용하여 이펙트를 조절합니다. 또한 Transform 속성을 이용하여 크기, 위치, 방향 등을 조절할 수 있습니다.

 

 

 

 

이제 충돌 시 해당 파티클 효과를 나타나게 해보겠습니다. 다음과 같이 PlayerController 스크립트를 수정합니다.

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private Rigidbody playerRb;
    public float jumpForce = 10;
    public float gravityModifier = 1;
    public bool isOnGround = true;
    public bool gameOver = false;
    private Animator playerAnim;
    public ParticleSystem explosionParticle;

    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody>();
        playerAnim = GetComponent<Animator>();
        Physics.gravity *= gravityModifier;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround && !gameOver)
        {
            playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            isOnGround = false;
            playerAnim.SetTrigger("Jump_trig");
        }        
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isOnGround = true;
        }
        if (collision.gameObject.CompareTag("Obstacle"))
        {
            gameOver = true;
            Debug.Log("Game Over");
            playerAnim.SetBool("Death_b", true);
            playerAnim.SetInteger("DeathType_int", 1);
            explosionParticle.Play();
        }
    }
}

 

 

ParticleSystem 컴포넌트를 찾아 Play 함수로 실행합니다. 매우 간단하지 않나요??

 

PlayerController의 ExplosionParticle 프로퍼티에다 해당 파티클을 부여합니다.

 

하나의 파티클이 더 있었죠. 이번에는 플레이어가 달릴 때, 땅에 흙먼지 이펙트가 발생하도록 해보겠습니다.

FX_DirtSplatter를 플레이어의 자식 오브젝트로 드래그하고, 위치를 변경하고, 회전하고, 설정을 편집합니다. 저같은 경우 Rotation.y만 180에서 90으로 변경해주었습니다.

 

 

PlayerController를 다음과 같이 수정합니다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private Rigidbody playerRb;
    public float jumpForce = 10;
    public float gravityModifier = 1;
    public bool isOnGround = true;
    public bool gameOver = false;
    private Animator playerAnim;
    public ParticleSystem explosionParticle;
    public ParticleSystem dirtParticle;

    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody>();
        playerAnim = GetComponent<Animator>();
        Physics.gravity *= gravityModifier;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround && !gameOver)
        {
            playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            isOnGround = false;
            playerAnim.SetTrigger("Jump_trig");
            dirtParticle.Play();
        }        
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isOnGround = true;
        }
        if (collision.gameObject.CompareTag("Obstacle"))
        {
            gameOver = true;
            Debug.Log("Game Over");
            playerAnim.SetBool("Death_b", true);
            playerAnim.SetInteger("DeathType_int", 1);
            explosionParticle.Play();
            dirtParticle.Stop();
        }
    }
}

 

 

이 역시 PlayerController 컴포넌트에 해당 파티클을 부여합니다.

 

 

 

 

 

이펙트가 완료되었네요! 이제 음향을 추가해볼까요?

 

기본적으로 음향이 들리는 기준은 MainCamera 입니다. 

 

MainCamera에 Audio Listener컴포넌트가 있다.

 

 

즉, 카메라에 가까울수록 음향이 크게 들리고, 멀어질수록 작게 들리게 됩니다. 이를 이용하여 배경음악은 카메라 오브젝트에 부여하는 경우가 많습니다.

Main Camera 오브젝트를 선택하고 Add Component > Audio Source로 이동합니다.
 

 

Course Library > Sound에서 음악 클립을 인스펙터의 AudioClip 변수로 드래그합니다.

 

 

 

 

 

 

배경음악이 너무 크네요. 음향을 줄여봅시다.  Volume을 0.4 ~ 0.5 정도로 설정합니다. 또한 오디오가 끝나면 다시 음악이 재생되지 않습니다. Loop을 체크하여 음악을 반복재생하게 합니다.

 

 

 

이제 점프소리와 충돌 소리를 제작해보도록 하겠습니다.

 

Player 게임 오브젝트에 AudioSource 컴포넌트를 붙입니다.

 

 

PlayerController를 다음과 같이 수정합니다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private Rigidbody playerRb;
    public float jumpForce = 10;
    public float gravityModifier = 1;
    public bool isOnGround = true;
    public bool gameOver = false;
    private Animator playerAnim;
    public ParticleSystem explosionParticle;
    public ParticleSystem dirtParticle;
    public AudioClip jumpSound;
    public AudioClip crashSound;
    private AudioSource playerAudio;

    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody>();
        playerAnim = GetComponent<Animator>();
        Physics.gravity *= gravityModifier;
        playerAudio = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround && !gameOver)
        {
            playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            isOnGround = false;
            playerAnim.SetTrigger("Jump_trig");
            dirtParticle.Play();
            playerAudio.PlayOneShot(jumpSound, 1.0f);
        }        
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isOnGround = true;
        }
        if (collision.gameObject.CompareTag("Obstacle"))
        {
            gameOver = true;
            Debug.Log("Game Over");
            playerAnim.SetBool("Death_b", true);
            playerAnim.SetInteger("DeathType_int", 1);
            explosionParticle.Play();
            dirtParticle.Stop();
            playerAudio.PlayOneShot(crashSound, 1.0f);
        }
    }
}

 

 

해당 스크립트에 생성한 jumpSound, crashSound에 맞게 점프소리와 파괴소리를 부여합니다.

 

 

 

이제 소리가 들어간 리얼한 게임이 완성되었습니다!