본문 바로가기
유니티강좌

2D 캐릭터 점프시 사운드 출력 방법(게임 만들기). 유니티 C# 스크립트 기초 강좌.

by Ncube 2020. 11. 28.

 

 

PlayOneShot 코드를 이용하여 사운드 출력. 한번만 플레이를 하고 동시에 여러개 출력하려면 audio.PlayOneShot(clip); 을 쓰면 되지만, Loop기능은 안된다. Unity C# Script(Insert Jump Sound)

 

#점프사운드 #PlayOneShot #AudioSource

 

[점프 사운드 경로] FreeSound : https://freesound.org/browse/

"Jump"로 검색해서 적절한 사운드를 찾아서 다운로드 받으면 됩니다.

 

 

[PlaySound.cs]

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

public class PlaySound : MonoBehaviour
{
    float moveX;

    Rigidbody2D rb;

    [Header("이동 속도")]
    [SerializeField] [Range(100f, 800f)] float moveSpeed = 200f;

    [Header("점프 강도")]
    [SerializeField] [Range(100f, 800f)] float jumpForce = 400f;

    public AudioSource mySfx;
    public AudioClip jumpSfx;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        moveX = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
        rb.velocity = new Vector2(moveX, rb.velocity.y);

        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (rb.velocity.y == 0)
            {
                rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Force);

                JumpSound();
            }
        }

    }

    public void JumpSound()
    {
        mySfx.PlayOneShot(jumpSfx);
    }

}

 

댓글