본문 바로가기
프로그래밍 _공부자료./C++ 공부

C# 유니티 오디오 컴퍼넌트 만들기.

by 대구부자 2022. 1. 13.
반응형
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource)) ] // unity 창에서 audio사운드 넣을수 있게.



public class Control : MonoBehaviour
{
    private float h = 0.0f;
    private float v = 0.0f;
    private float r = 0.0f;
    private float moveSpeed = 10.0f;
    private float rotationSpeed = 100.0f;
    private Transform playerTr;
    private int key;
    public AudioClip ksySfx;
    private AudioSource audioSource; //오디오 소스 넣을수 있게 unity 배너 추가.

    // Start is called before the first frame update
    void Start()
    {
        playerTr = GetComponent<Transform>();
        audioSource = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        h = Input.GetAxis("Horizontal");
        v = Input.GetAxis("Vertical");
        r = Input.GetAxis("Mouse X");
       // Debug.Log("H :" + h.ToString() + "V" + v.ToString());
        playerTr.Translate(new Vector3(h, 0, v) * moveSpeed * Time.deltaTime); //Time.deltaTime --> 컴퓨터 성능따라 다르므로 시간 값 같게???
        playerTr.Rotate(new Vector3(0, r, 0)* rotationSpeed * Time.deltaTime);
    }
    private void OnCollisionEnter(Collision collision)
   {
        if(collision.gameObject.tag =="Key")
        {
            Destroy(collision.gameObject);
            key++;
            audioSource.PlayOneShot(ksySfx,1.0f ); // 오디오 소스 실행
            Debug.Log("key 개수  :" + key.ToString() );
        }       
    }
}

 

 

public으로 audioClip 선언시 아래와 같이 생성됨.

 

반응형

댓글