Slight lag on collision sounds

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.

Hi Chironex,

Welcome to our community! :slight_smile:

First of all, good job on doing research by yourself on this problem. I couldn’t have done this better.

Maybe you already know this but if not, it won’t do any harm if I mention it here. In Unity, we have got different Unity methods which get called and executed at different points in time. See here. Update and the Collision methods are not in the same cycle.

To analyse the problem a bit more, add one Debug.Log to the Collision method. Log Time.frameCount into your console along with a meaningful message. Play your game. When the ball is about to collide, monitor the messages in your console. Is there a noticeable delay between the appearence of the message and the sound? If not, the problem might be the optimised physics simulation. While you see the ball colliding, its collider has not collides yet. No collision, no OnCollision method call.

If that’s the case, you could try to set the Collision Detection of the player’s Rigidbody2D component to “Continuous”.

Thank you so much Nina. I tried it. Setting collision detection to Continuous definitely decreased the lag. It was really bugging me, that lag. :smiley:

I have one more question. Why don’t we always keep this on? Is it more expensive for processing power so in larger games, it would cause slowdown? Or is there an advantage to keeping it discreet?

For reasons of performance. Unity’s physics simulation does not recalculate everything every frame. However, for fast moving objects, it could make sense to have the simulation calculate the position more often than usual for a specific object.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms