Need help drawing a rectangle in the HUD

I know this is probably super simple and been asked a few times, but I can’t quite find anything about my exact symptoms. I’m just trying to draw a simple rectangle to select actors in the level, like in an RTS.

Here is my setup:

This is in my player controller BP. EnhancedInputAction IA_SelectBegin’s Triggered pin fires when I click LMB. The triggered pin on the EnhancedInputAction IA_SelectEnd fires when I release LMB, and the Ongoign pin fires when I am holding LMB down.

The following are all in my HUD BP.

The end result is that when I just click on the screen, a black rectangle of arbitrary dimensions appears. The closer I click to the top left corner, the smaller it is. If I try to drag the mouse, the rectangle jitters a bit but I can’t make it significantly larger or smaller and it snaps back to its original size. I have debugged by printing the mouse coordinates on the screen and found that while I am dragging it, no matter how far away I move, it constantly reverts back to the point where I initially clicked.

Are you trying to to Select Units RTS type of rectangle or something else?

If you are doing this on HUD you can do like this, pass the WorldPosition of the cursor position from your pawn or controller or anywhere you like

And in your hud draw it , this will simply draw a rectangle on HUD level.

It should show like this

1

You can also directly on user widget too. For that you can do something like this

It will show like this

2

Ofcourse if you are talking about top down, either you can make a new render aligned to camera or simply you can draw a transparent plane in the world. Also just in case want to mention that selection will need to match your world so you can select the units.

There are couple of ways to do that for prototype or more advanced methods, like projecting on to a topology or a complex shape, some of them render based (mesh , decal , shader) some of them object based (which is better in some terms since selection also can be done at the same time) so there are mix of methods if you want AAA ones however at some point C++ required. Think we can do with geometry scripts too a simple one. Let us know.

1 Like

triggered is usually called on tick so you’re constantly updating the mouse position start. you dont need the hold at all since DrawHUD is also called on tick so you can move the bool there and get mousepositionend there too

1 Like

So today I check a little bit if we can do this over geometry scripts with pure blueprints to a certain level and managed to create a decent actor.

1- So actor has a box collision and a dynamic mesh component. On begin play we initilze mesh ,simply adding a quad, creating a material instance and adjusting its pivot to be top left.

2- Created an event with OnDrawingStatus bool status and this simply passes 2 vector parameters, starting pos and current pos. When this is true we start we start transforming vertices. Gets we add adjacent positions to veritces on local space.

3- When OnDrawingStatus false we finalize the dynamic mesh, exture it in Z axis, recompute UV and normals just incase, and place /resize our box collision perfectly inside, additionally we spawn a decal for a better feedback to player.

4- We spawn actor from Input or any other place preffered. Pass start and CurrentPos with input updates to actor.

Results are a simple but quite nice RTS unit selector that can be extended / scaled further.

3

1 Like

looks good, what i like about this is you can use overlaps instead of GetActorsInRect which runs on tick and its already in world space

1 Like

Yeah thats actually even better, depends on the game and interaction needs though sometimes.

So I played with this a bit more

Again calls an actor to do stuff for now, passes mouse position at the moment but can be doing its own detection in the future maybe.

Actor spawns, Initilizes a mesh, binds to events of overlaps and creates some feedbacks on units selected for now, which communication and management can be on other system later on.

And starts translating vertices, a bit went further here, no additional component just dynamic mesh and we batch transform all the cube vertex. This gives ofcourse greater control over box size, Z height without any additional component needed. Maybe a bit over engineer but it feels nice and performing since its 8 vertices updated with additional collision reformation.

While the Dynamic mesh changing we already get the overlaps, record them, I additionally put for now an overlay on them an remove if they get out of the collision just during selection. Which again can be manager that handles the selections states of units in another class rather than squeezing into actors lifetime.
4

During selection made a grid divide current area and line trace surface and save hits.

This is good for couple of things imo.

1st I don’t have to use decals, I kinda find them clunky for this type of interaction.
2nd option is using HLSL or PostProcess to indicate intersection with stencils. Stencils not exactly can work with this, HLSL we can kinda do the bordering and maybe a cap to it however in a dynamic scenario (landscape, props) its limited.

5

So I go with the other option which I think it will be better long term especially for AAA game scenario. Since I have the points we can do couple of things.

1- We can create a proxy mesh on top. I was able to achieve this actually after lots of vertice math and triangle creation however on tick its heavy.
2- We can create a new custom shader with actual points and draw a proxy which would be the best. However its kinda over engineering already overengineered proto but its there, can be done that way.
3- So I preffered instanced static meshes at those points, which is not render heavy, looks nice and opens room for artistic expression aswell. We can even prefer to create spline meshes with points as grid.

6
Also scripts and demo video available here.

@davi1521 Thanks for the question by the way, provided multiple answers but this one is actually something that is more overkill. Let us know if your question answered in some terms or fully.

Incase anyone stumbles on this, wrapped this approach into a plugin. Without complex geometry scripting or raycasts but just by collisions and cosmetic representation on gpu level with post process which reacts to terrain nicely.

1 Like

looks amazing, cheers

1 Like