HUD/Canvas - Drawing text or a texture/material from centre?

Hey guys,

Imagine you’ve cast a ray from the front of your player into the world, and using the impact point, once projected onto the HUD, as a position at which to draw your crosshair. The crosshair may be scaled for some reason (bigger = stronger bullets? Who knows), but you want it centred around the given point found above.

In this example, how do you make the crosshair texture draw around the screen coordinate? At the moment, you need to hard-code a value for the size of the texture / 2 (i.e. make sure you always use a 64 x 64 image for it, and thus hard-code a value of 32) multiplied by the used scale (which may change as you go), and then subtracting that from the projected position’s X and Y to adjust the image to draw from the centre at the given scale.

This is a horrible way to achieve something simple. Does the canvas have a built-in way of doing such a simple operation? I’d like to draw text or textures around a given screen coordinate.

Thanks.

You can get the size of your image and divide it by two.

For this you will need the texture as variable but then you can read this value.

If you use text there is also an option to center it (only the align), I forgot the exact name of the node but you should be able to find it if you search the defaults of a text render.

Normaly you would check the x and y value of the texture (there is a node for it “get x” or something) and substract the half of each of this from the screencoordinates half.

So in math it would be:

XPosition = ScreenX / 2 - TextureX / 2;
YPosition = ScreenY / 2 - TextureY / 2;

ScreenX, ScreenY and the two TextureX and Y values are easy to get in blueprints.

The screens one are given by the Draw HUD and the Texture ones can be get by the nodes i mentioned above.

If you change your crosshair, than it will take the new TextureX and Y, so it will always stay in the middle of the screen.