Hi! I’ve been following along the Unity 2D course. I have reached the block breaker section. I find that there is a slight lag between the collision and the sound that is playing. The audio file is attached to audio source of the ball object.
The same code as is shown in the course:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
// Config Parameters
[SerializeField] Paddle paddle1;
[SerializeField] float startVelocityX = 2f;
[SerializeField] float startVelocityY = 10f;
// state
private Vector2 paddleToBallVector;
bool hasStarted = false;
private void Start()
{
paddleToBallVector = transform.position - paddle1.transform.position;
}
private void Update()
{
if (!hasStarted)
{
LockBalltoPaddle();
LaunchBallOnMouseClick();
}
}
private void LaunchBallOnMouseClick()
{
if(Input.GetMouseButtonDown(0))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(startVelocityX, startVelocityY);
hasStarted = true;
}
}
private void LockBalltoPaddle()
{
Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
transform.position = paddlePos + paddleToBallVector;
}
private void OnCollisionEnter2D(Collision2D collision)
{
GetComponent<AudioSource>().Play(0);
}
}
Everything works fine. But there is a lag between the collision and the sound. It is a small interval. Much less than 1 second, but clearly audible.
Here’s what I have tried till now:
The file is the SFXClick file that is included in the lesson. I opened it in Audacity and found that there is not much open space before it. I shortened it anyway. That didn’t help
Next, over the internet, I found some conversations saying to preload audio data. I found in my Unity inspector for the sound that that is already on. I tried with ‘Force to mono’, Load in Background’ and by decreasing quality of the clip. None of them decrease the lag.
Next I found a conversation that asked to check sound latency by coding some sound on mouseclick. So attached a GetComponent.play to my Input.GetButtonDown function. This time I hear click as soon as I click. So there is no lag this way.
Help would be welcome. I did read the unity documentation before asking. I’m really new to all this. Have no programming experience. Sorry if I am overlooking something stupidly simple.