유니티 오디오

효과음을 내기위한 세 가지

Audio Listener

- 오디오를 듣기

Audio Source

- 오디오를 플레이 하기

Audio File

- 재생되는 소리

 

Audio Listener

메인 카메라에는 기본적으로 Audio Listener가 들어있는데 기본 설정상 우리가 보는 화면이기 때문이다.

메인 카메라 오디오 리스너

오디오 소스나 오디오 리스너는 다른 컴포넌트처럼 오브젝트에 추가해서 사용한다.

오디오 소스

Audio Source

오디오 소스 컴포넌트는 오디오 클립을 추가해서 플레이 하는 식으로 동작한다.

- Play On Awake : 시작할 때 켜짐

- Loop : 반복

오디오 소스 2

 

Audio File

- 오디오 파일 다운로드 사이트 (저작권 주의)
https://freesound.org/

 

Freesound - Freesound

Freesound Blog Recent Additions Woodpecker pecking on a tree, slight wind shaking foliage, birds singing, far plane passing and road hum at the end ... 13 more sounds from felix.blume in the last 48 hours 4 more sounds from josefpres in the last 48 hours 2

freesound.org

- 직접 효과음 녹음하는 사이트

https://www.audacityteam.org/

 

Home

Welcome to Audacity Audacity® is free, open source, cross-platform audio software for multi-track recording and editing. Audacity is available for Windows®, Mac®, GNU/Linux® and other operating systems. Check our feature list, Wiki and Forum. Download

www.audacityteam.org

 

오디오 파일은 이런식으로 에셋 폴더에 넣고 누르면 확인이 된다.

오디오 파일을 Audio Source 컴포넌트의 Audio Clip에 드래그 앤 드롭다운 해서 넣으면 플레이할 준비가 된 것이다

오디오 클립


Movement.cs

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

public class Movement : MonoBehaviour
{
    private Rigidbody rigid;
    private AudioSource audioSource;
    [SerializeField]
    private float mod = 100f;
    [SerializeField]
    private float rotationSpeed = 1f;
    void Start()
    {
        rigid = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
    }

    void Update()
    {
        ProcessThrust();
        ProcessRotation();
    }

    void ProcessThrust()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            rigid.AddRelativeForce(Vector3.up * mod * Time.deltaTime); // Time.deltaTime은 1프레임당 시간
            // 재생하고 있지 않을 때만 재생 (재생이 겹치는 걸 막음)
            if (!audioSource.isPlaying)
                audioSource.Play();
        }
        else
            audioSource.Stop();
    }

    void ProcessRotation()
    {
        if (Input.GetKey(KeyCode.A))
        {
            // Ctrl + .으로 반복되는 문장을 함수로 빠르게 만들 수 있다.
            // 반복되는 문장은 다른 것만 매개변수로 받으면 좋다.
            // Ctrl + R 두 번으로 이름을 한 번에 수정할 수 있다.
            ApplyRotation(rotationSpeed);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            ApplyRotation(-rotationSpeed);
        }
    }

    private void ApplyRotation(float rotationThisFrame)
    {
        // 비트연산이라 더하면 xy 값 물리적 고정과 같음
        // rigid.constraints = (RigidbodyConstraints)((int)RigidbodyConstraints.FreezeRotationX + (int)RigidbodyConstraints.FreezeRotationY);
        // 물리적으로 회전 고정
        // rigid.constraints = RigidbodyConstraints.FreezeRotation;
        
        rigid.freezeRotation = true; // 회전 값을 고정해서 수동으로 회전을 조작할 수 있게 함
        // 이게 되는 이유는 rigid(물리)충돌의 회전만 제어했을 뿐이지 transform 기반으로 한 회전은 수동으로 제어 가능 
        transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);
        rigid.freezeRotation = false; // 회전 값 고정을 풀어서 물리 시스템이 적용 됨
    }
}

'유데미 강의 > C#과 Unity로 3D 게임 개발하기 : 부스트 프로젝트' 카테고리의 다른 글

SceneManager  (0) 2022.08.12
Switch  (0) 2022.08.12
소스 컨트롤  (0) 2022.08.12
오브젝트 회전과 매개변수와 Rigidbody  (0) 2022.08.04
부스터  (0) 2022.08.03

+ Recent posts