Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- /**
- * STEPS:
- * 1) Create an empty cube and add it to the scene
- * 2) Add 'AudioSource' to the cube
- * 3) Add 'playsfx.cs' to the cube
- * 4) Add your sound effect (in my case, explosion.mp3) to that script
- * 5) Add your sound effect (in my case, explosion.mp3) to your AudioSource
- * 6) Hit 'play' in Unity
- */
- public class playsfx : MonoBehaviour {
- // Add a reference to our sound effect
- public AudioClip shootSound;
- // Where is this coming from? Did you add 'AudioSource' to your cube?
- // Did you drag your .mp3 to the AudioSource?
- private AudioSource source;
- // Use this for initialization
- void Awake () {
- source = GetComponent<AudioSource>();
- }
- // Update is called once per frame
- void Update () {
- // If user hits left-mouse button.....
- if (Input.GetButtonDown("Fire1"))
- {
- // Check if there is a sound effect...
- if (shootSound == null) {
- // Throw a warning. Whoops, forgot to add a sound effect!
- Debug.Log("You forgot to add a sound effect! " + shootSound);
- }
- // Play sfx
- source.PlayOneShot(shootSound);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement