Showing moving Sprite2D on top of UI elements?

Godot Version

4.4.1.stable

Question

My Pixel Art project has a custom sprite for the mouse cursor. I created a Scene containing a Sprite2D -with the desired sprite- and its position is set to get_global_mouse_position() on _process(), this is because setting the custom mouse sprite via Project Settings resulted in a very small cursor sprite compared to the rest of the game’s assets, and it didn’t respect the project’s pixel density.

I want my custom mouse sprite Scene to render on top of some UI elements, and these elements are inside of a CanvasLayer:
Showcase

I have tried adding a CanvasLayer as a parent of the Sprite2D of the custom mouse Scene, but then it stops moving and stays still at the top-left corner of the DEBUG window, or at the center of it if its CanvasLayer’s Follow Viewport is set to Enabled.

How can I achieve this?

(This is a crosspost between the Godot Forum and the Godot Subreddit. Godot Subreddit post link: https://www.reddit.com/r/godot/comments/1kaphu6/showing_moving_sprite2d_on_top_of_ui_elements/)

Edit: This is solved! I’m going to share an example project for anyone who might need it in the future: ExampleProject.rar - Google Drive

Have you tried setting the Z-index for the cursor sprite?

The project settings has a field for a custom mouse cursor image. Did you try that?

Yes! But CanvasLayers function independently from Z-Index… As much as I modify this value, the UI’s CanvasLayer will always have priority over Z-Index.

I already have, yes! ^^

You could move your mouse scene to the UI canvas-layer and then set the z-index accordingly.

You say you already tried moving it to its own canvas-layer but it does not work. How are you setting the position when the sprite is inside the separate canvas-layer? Make sure you set the position of the element inside the canvas layer bases on its get_global_mouse_position.

1 Like

Thanks to an example project from a Godot Subreddit user, I managed to make progress and achieve what I wanted!
I’m going to share an example project for anyone who might need it in the future: ExampleProject.rar - Google Drive