- Home /
Audio playing when not colliding issue c#
I have a script that has a function that I need to both play audio when not colliding and stop playing audio when I do collide. But I don't know how to call my function in update, and I don't know how to use Stop, and I can't find out how anywhere in unity's scripting library. Please help!
using UnityEngine; using System.Collections;
public class Falling : MonoBehaviour { public AudioClip FallSound;
private AudioSource Source;
private float volmin = .5f;
private float volmax = 1.0f;
void Start () {
Source = GetComponent<AudioSource>();
gameObject.GetComponent<Rigidbody2D>().freezeRotation = true;
transform.position = new Vector3 (0f, -0.7f, -1f);
}
void FallPlayerSound (Collision col){
if (col.gameObject.tag == "Obstacle") {
//Stop playing the sound? Somehow?
} else {
float vol = Random.Range (volmin, volmax);
Source.PlayOneShot(FallSound, vol);
}
}
void FixedUpdate () {
if (transform.position.y > 3.75) {
Destroy(gameObject);
}
if (transform.position.y < -4) {
Destroy(gameObject);
}
//I guess I should call FallPlayerSound here? But how? And with what arguments?
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220611234828im_/https://answers.unity.com/themes/thub/images/avi.jpg)