World space canvas while not being affected by screen shake

Is it possible to have a canvas set to ‘world space’ while not being affected by screen shake?

I have a UI text inside a canvas with the render mode set to ‘world space’, the camera is it set to orthographic and it’s a 2D game. And I’m using cinemachines 6D noise profile to add screen shake, by simply changing the parameters with this following script:

    void Update() {
        if (_virtualCamera != null && virtualCameraNoise != null) {
            if (ShakeElapsedTime > 0) {
                virtualCameraNoise.m_AmplitudeGain = ShakeAmplitude;
                virtualCameraNoise.m_FrequencyGain = ShakeFrequency;

                ShakeElapsedTime -= Time.deltaTime;
            } else {
                virtualCameraNoise.m_AmplitudeGain = 0f;
                ShakeElapsedTime = 0f;
            }
        }
    }

    public void ShakeCamera(float shakeDuration, float shakeAmplitude, float shakeFrequency) {
        ShakeElapsedTime = shakeDuration;
        ShakeAmplitude = shakeAmplitude;
        ShakeFrequency = shakeFrequency;
    }

The problem is when I’m running this script, the UI texts are affected by the shake as well, which i don’t want. I can provide an example if further explanation is needed.

Shaking the camera means that it moves in the world. If your canvas is attached to the world then it will appear to move relative to the camera. That’s why there’s an option to build the canvas in canvas or screen space - so it stays still on the screen when the camera moves. Why don’t you use that?

If you really must have your canvas in world space, and don’t want it to appear to move when the camera does, then you’ll have to shake the canvas along with the camera.

Yeah ok, thanks. The texts are placed on specific locations around the level, and i don’t want them to follow the camera. So i figured world space would be the right choice to use. I suppose that’s not possible with the screen space options?

If you want the UI elements to be attached to world points, then you’re pretty much stuck with world space camera mode. So that leaves the option of shaking the UI elements in sync with the camera.

If you use CM’s Impulse module instead of the Noise component, then you can do that quite easily. Have an impulse emitter somewhere in your world. Emit an impulse from there when you want a shake to happen. Put an impulse listener on the camera, and an impulse listener on your canvas. If you put the same settings on both, they will shake exactly the same way in response to the impulse signal, and since your camera is ortho, there won’t be any perspective problems to contend with. It might just work.

Note: you’ll have to craft a signal that consists only of positional shake - not rotational shake, or it won’t work properly. The 6D Shake signal has both. You can clone it and remove the rotational components.

1 Like