Blueprints OnClicked only working at close range

Hi all,

I am new to UE4 and have been trying to make a first-person game where the player can click on actors to destroy them. I made a destructible actor blueprint and successfully made some code to apply damage. I connected this code to the event actor OnClicked. This is what it looks like:

When I go really close to the actor and click, it is destroyed, but any further away and nothing happens. I have not set any values related to a minimum distance. Is there one by default? Any ideas as to what may be causing this?

EDIT: Having done some more testing, I found even more weird behaviour. If I run around the world clicking on the actor, it breaks randomly. By this I mean sometimes it just breaks when I first click it, no matter the distance, and other times I have to click on it a lot and then it suddenly breaks. It does work, but just really inconsistently, not registering most clicks.

Thanks for your support!

1 Like

There’s trace distance in the PlayerController > ClassDefaults > MouseInterface tab. It defaults to 100k which is quite a lot but you may want to crank it up, ofc. You can also enable click events there permanently.

You are currently using Visibility channel for this (I think that’s the default) which is fine but you can also set up custom trace channels that ignore certain objects so nothing ever interrupts your traces (think about clicking on something under the surface of water while being on the boat, you’d would’t want the water to be in the way). This is a good read if you want to explore things further.

Also, the user interface can intercept and consume mouse input. Could you post an example screenshot of the scene where the desired functionality fails, even if it isn’t 100% reproducible?

When it comes to objects getting randomly destroyed, that’s a bit weird, indeed. Could you tell a bit more about the destructibles you’re using for this, how is the collision set up?

2 Likes

Really sorry, I am a bit of a noob with UE4. Would you mind telling me what you mean by the PlayerController>ClassDefaults>MouseInterface tab and how do I get there? Also when you say a screenshot of the scene do you just mean a picture of the game while running? As to the randomly destroyed what I meant by that was that some of my clicks had an effect, and some of them didn’t and there didn’t seem to be a pattern to it. Sorry if I was unclear.

Thanks for your help!

Since you’re new to the engine, you should definitely consider looking into the framework first:
https://docs.unrealengine.com/latest/INT/Gameplay/HowTo/SettingUpAGameMode/Blueprints/

The above describes how to set up a custom GameMode, it’s a managing class that handles the PlayerController which, in turn, handles all the input from the player. You can’t really do anything serious without them.

Once you have the GameMode set up, create a new PlayerController blueprint in the ContentBrowser and assign it to the GameMode here:

Once you have it, open the PlayerController blueprint and you can find the tracing distance here:

As you can see, mine is 10x the default as I’m dealing with huge distances. I think that 1uu (UnrealUnit) is 1cm, so the default 100k = 1km - enough for most cases. You problem may lay elsewhere, that’s why I’m curious regarding the following:

Also when you say a screenshot of the
scene do you just mean a picture of
the game while running?

Yeah, just to see how dense the scene is or what else could be in the way of your mouse clicks.

1 Like

Thank you so much! I will try this out and let you know if I have success

EDIT: Sorry! I have just located my problem. The actual cursor is in a different location on the screen to my HUD cursor I created. Therefore, the reason it works better when I am closer to the cube is that the cube takes up more of the screen and you have a higher chance of clicking on it with the real cursor. Thank you so much as it was your steps that took me to the answer. Unfortunately I am unable to find a way to actually fix the problem. Is there a way I can lock the mouse cursor to the centre of the screen, or a way that I can use the centre of the screen as a mouse cursor?

Sorry, my edits got a bit confusing. I actually just added to this comment saying that no, I have not fixed it. I am unable to find a way to either lock the mouse cursor to the centre of the screen, or use the centre of the screen as the mouse cursor. Is there a way I can do one of these? Thank you!

Having said all that, I am not 100%
sure that I set up the player
controller correctly. Is there any way
I can easily test if the player
controller is being used?

Open the PlayerController blueprint and do something like this, if you get Hello printed in the upper left corner every time you click, it’s set up correctly.

219208-capture.png

Also, when you look at world settings, you should be seeing both the GameMode and the controller:

I am editing the comment below a few
minutes after posting and have located
my problem. The actual cursor is in a
different location on the screen to my
HUD cursor I created. Therefore, the
reason it works better when I am
closer to the cube is that the cube
takes up more of the screen and you
have a higher chance of clicking on it
with the real cursor. Thank you so
much as it was your steps that took me
to the answer.

Does this mean it’s actually working as intended now?

I am unable to find a way to either
lock the mouse cursor to the centre of
the screen, or use the centre of the
screen as the mouse cursor. Is there a
way I can do one of these? Thank you!

Essentially, it sounds as if you wanted to identify objects by looking directly at them. Yeah, it’s pretty straightforward:

This is the most basic form of line tracing, it shoots a line from the camera location along the direction in which it is facing and checks whether anything visible gets in the way - you get plenty of information about the hit object once you break the struct.

If you’re unhappy with the implementation of the onClicked event in your cubes, you can try this:

I put this in the Pawn, you can put it in the character or the player controller where it actually belongs. It detects clicked on objects and destroyes them.

I have now fixed everything! I used a line trace in my character blueprint as you showed and connected that to the apply damage function to destroy my destructible mesh. Thank you very much for all of your help! If I ever do finish my game I am sure you will get yourself a free copy.

Oh, wow. Lot’s of progress made. Congrats and thanks!
Consider marking this as answered, helps others find theirs answers quicker.