Doppler effect/relative velocity with Cinemachine camera does weird things?

I have the studio listener tied to the camera which is being controlled by cinemachine + additional scripts.

I was trying to get the doppler effect working with a stationary object (with a studio emitter attached and Allow Non-Rigidbody Doppler enabled), and also trying to use the relative velocity between object and listener.

Initially the relative speed was stuck at 0 with no doppler effect, and the only way I could get it to react at all was to add a rigidbody to the camera object (where the listener was also attached). This does some pretty wild stuff though, and the relative velocity oscillates constantly, even if the camera is completely stationary.

I’m guessing this is due to the way the camera’s position is updated making it impossible to calculate the correct relative speed/distance between it and the object? Is there a workaround for this without needing additional scripting? Sticking the listener on the player object makes everything work perfectly, and makes implementation of the doppler effect very easy, but unfortunately we need the listener on the camera!

Hi,

Thank you for bringing this to our attention. I was able to reproduce the issue. I have passed it on to our development team to look into further.

Unfortunately, there isn’t a workaround without code changes. I will update this thread when there are updates.

Thanks for confirming this. You mention there’s a workaround with code changes, is that on the Unity side, and if so would you be able to share what these would be? The programmer and I were experimenting with sending this in LateUpdate (to try and override whatever the StudioListener/Emitter was doing) but had no luck:


 Vector3 velocity = (_camera.transform.position - _lastFramePosition) / Time.deltaTime;
        RuntimeManager.StudioSystem.setListenerAttributes(0, RuntimeUtils.To3DAttributes(gameObject.transform, velocity));
_lastFramePosition = _camera.transform.position;

I have implemented a workaround doppler effect myself but in the limited time I’ve had with it I haven’t been able to get it to sound as nice as the FMOD implementation so if there was a way to get the built in version working that would be awesome.

Hi,

With these changes, all listeners will have Doppler enabled:

Change the SetListenerLocation in RunTimeManger.cs:

public static void SetListenerLocation(int listenerIndex, GameObject gameObject, GameObject attenuationObject = null, Vector3 velocity = new Vector3())
{
    if (attenuationObject)
    {
        Instance.studioSystem.setListenerAttributes(listenerIndex, RuntimeUtils.To3DAttributes(gameObject.transform), RuntimeUtils.ToFMODVector(attenuationObject.transform.position));
    }
    else
    {
        Instance.studioSystem.setListenerAttributes(listenerIndex, RuntimeUtils.To3DAttributes(gameObject.transform, velocity));
    }
}

Then add

private Vector3 lastFramePosition = Vector3.zero;

to StudioListner.cs and replace lines 99 - 137 in with:

private void Update()
{
    var position = transform.position;
    var velocity = Vector3.zero;

    if (Time.deltaTime !=  0)
    {
        velocity = (position - lastFramePosition) / Time.deltaTime;
        velocity = Vector3.ClampMagnitude(velocity, 20.0f);
    }

    lastFramePosition = position;

    if (ListenerNumber >= 0 && ListenerNumber < FMOD.CONSTANTS.MAX_LISTENERS)
    {
        SetListenerLocation(velocity);
    }
}

private void SetListenerLocation(Vector3 velocity)
{
#if UNITY_PHYSICS_EXIST
    if (rigidBody)
    {
        RuntimeManager.SetListenerLocation(ListenerNumber, gameObject, rigidBody, attenuationObject);
    }
    else
#endif
#if UNITY_PHYSICS2D_EXIST
    if (rigidBody2D)
    {
        RuntimeManager.SetListenerLocation(ListenerNumber, gameObject, rigidBody2D, attenuationObject);
    }
    else
#endif
    {
        RuntimeManager.SetListenerLocation(ListenerNumber, gameObject, attenuationObject, velocity);
    }
}

Please note this is just a workaround and once there is an update with a fix I will post it here.

Awesome, thanks so much Connor really appreciate it

1 Like