I have a ball object, that, when hit something, should play a sound. And it does, but not consistently (one time it can play it every time it hits anything like intended, but the other times sound only plays on some hits). In attempts to solve this, I tried lowering fixed timestep, but it doesn’t seem to work.
Here are relevant parts of the code.
Ball.OnCollisionEnter2D:
private void OnCollisionEnter2D(Collision2D collision) {
Debug.Log("Hit anything");
OnBounced?.Invoke(this, EventArgs.Empty);
}
SFXManager:
using UnityEngine;
public class SFXManager : MonoBehaviour {
[SerializeField] AudioClipsData audioClipsData;
private void Start() {
Ball ball = Ball.Instance;
ball.OnBounced += Ball_OnBounced;
}
private void Ball_OnBounced(object sender, System.EventArgs e) {
PlaySound(audioClipsData.bounce, Camera.main.transform.position);
}
private void PlaySound(AudioClip audioClip, Vector3 position, float volume = 1f) {
AudioSource.PlayClipAtPoint(audioClip, position, volume);
}
}
To answer some probable questions: Yes, OnCollisionEnter2D logs “Hit anything” consistently, even when no sound plays. But sound isn’t playing for some reason.
I also apologize, if it’s a wrong part of forum to post this.