Skip to content

Instantly share code, notes, and snippets.

@WakkyFree
Created August 21, 2017 19:18
Show Gist options
  • Save WakkyFree/99bc0d0d7fea4ac37ff381134a873caf to your computer and use it in GitHub Desktop.
Save WakkyFree/99bc0d0d7fea4ac37ff381134a873caf to your computer and use it in GitHub Desktop.
Unity script to enable sound effects when a bat hits a ball.
using UnityEngine;
using System.Collections;
public class SoundBat : MonoBehaviour {
private AudioSource audioSourceBat;
public AudioClip seBat;
// Use this for initialization
void Start () {
audioSourceBat = gameObject.GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.name == "Ball") {
audioSourceBat.PlayOneShot(seBat);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment