LoginSignup
0
1

More than 3 years have passed since last update.

【Unity】クリックしたら音が鳴るようにする方法(自分用)

Posted at

右クリックしたら音が一回鳴るようにするやり方。

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

public class Test : MonoBehaviour
{
    AudioSource _audiosource;
    public AudioClip sound;

    void Start()
    {
        _audiosource = GetComponent<AudioSource>();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            _audiosource.PlayOneShot(sound);
        }
    }
}

このスクリプトを適当なオブジェクトにアタッチ。
インスペクターに鳴らす音源をドラッグ&ドロップ。
AudioSourceをつけるのを忘れずに。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1