Top Down View Left Button Move And Interact

Hello!
I’m new to Unreal and I’m starting a top-down view project. I would like my left mouse button to move my character (as it is already doing in the base template of Unreal) to the location of my cursor if I click on the floor. And if I click on something interactable, I would like my character to move to this object and start whatever the interaction is.
I could not find any tutorial on how to do this.
I know how to trigger the interaction if I assign it to another button. But I would really like my left button to do both.
I guess I would need to check first on what I’m clicking and if this object has the interaction interface implemented, and then decide on the action to perform but I am not sure this the right way. Also, I do not know where to write this (inside of my top down controller blueprint or inside of my top down character bluprint).
Would you know how to help me?

1 Like

I think the way to do it is to have an overlap volume on the interactive actor. That way, when your character gets there, you can send a message for interaction ( or your character can find it ).

You just click the point on the floor, the player walks over to it. The player has a begin overlap event, and then checks the other actor for the interface.

Thank you for your answer.

The thing is, I don’t want dialogues or other things to trigger whenever I get close to the NPC/Object. I want my players to specifically click on them. Besides, in the situation you are mentioning, my character would be asked to do two things at once: walk to the object + trigger the event at the same time (since right now, my left click is “move to cursor” even though there is an object under cursor, I would need my character to stop near the object, then stop, then trigger event (I don’t know if that makes sense)).

So wouldn’t that be a problem? Unless, my event trigering can override my ‘moveto’? I don’t really know if that’s possible?

I found that ‘GetHitResultUnderCursorforObject’ h(ttps://docs.unrealengine.com/en-US/BlueprintAPI/Game/Player/GetHitResultUnderCursorforObject-/index.html) might be what I’m looking for but I can’t seem to make it work. This way, I would check in my topdowncontrollerblueprint (not sure that’s the right place to do this?) what is under my cursor, and if it’s an interactable I moveto>stopinrange>interact and if it’s not i just ‘moveto’.

If someone has an idea on how to do this this way, or another way, that would be great!

In the Top Down template, the click-and-move functionality is set up in C++ – I can’t remember where they put it, but I seem to remember it being in the TopDownCharacter class itself. The way it works is that they bind the left mouse click input to a function that gets the Hit result under the cursor, gets the location of the HitResult, and calls a move function – I think it’s the SimpleMoveToLocation function or something like that – that takes the character to that location. The simple solution for you would be to override that bound function so that you get the HitResult under the cursor, then get the actor that is the subject of that HitResult, and try to cast it to whatever you would interact with. If you can cast it to that interactable thing, then call whatever function you are using for the interaction. If you can’t cast it to anything interactable, get the location of the HitResult and call the same move function that the original function calls.

The game I’m currently working on does exactly what you are describing. I basically removed the binding and move functionality that was in the TopDownCharacter and put all that into my PlayerController class. In my custom player controller class, I override SetupInputComponent as follows to bind the left mouse click:

void AQuestPlayerController::SetupInputComponent()
{
// set up gameplay key bindings
Super::SetupInputComponent();

InputComponent->BindAction("SetTarget", IE_Pressed, this, &AQuestPlayerController::OnSetTargetPressed);
}

Then, I define and declare OnSetTargetPressed. Mine is complicated, but essentially, you want something like this:

{
FHitResult Hit;
if (GetHitResultUnderCursor(ECC_Visibility, false, Hit))
{
if (Hit.bBlockingHit)
{
[do whatever you are going to do here]
}
}
}```

In terms of what you do in the "[do whatever you are going to do here]", mine gets complicated, but here is a simplified version of what you would do to interact on a cat but otherwise move if you didn't click on a cat:

```AActor* HitActor = Hit.GetActor();
AMyCat* MyCat = Cast<AMyCat>(HitActor);
if (MyCat)
{
[call whatever function you would call to interact with the cat]
}
else
{
[call whatever function you would call to move the character; you may want to copy what the TopDownCharacter.cpp does to move the character]
}
}
Sorry I don't remember exactly how the TopDown template works, but this is the idea. You should be able to look at the TopDownCharacter.cpp and see what it's doing and then modify it as I've described above.

I realize this is a Blueprint topic, so you may not be using C++. I haven't tackled this in Blueprint, but it's going to be the same idea -- bind the left mouse click to a function that checks to see what you clicked on and then does what you want based on what you clicked on.

Sorry my post didn’t block the code – I used the three back quotes, which I thought would work, but apparently I have forgotten how to block the code.

Sure, so just use the default ( which moves the player where you want ), and add code to each object you want to click.

GetHitResultUnderCursor is fine ( channel or object ). You’ll need to enable click events in the controller ( probably already done ), and input in each object:

Hello and thank you for your answers. I can’t make it work unfortunately. Player movements (move on click) are handled in my player controller, so I guess this is where I would need to add code so it does : click - check if i am clicking on an actor that can be interacted with - if it’s the case - walk there - and interact (if not, just walk there).
What happens right now is that I can launch my interaction when I click on interactables but somehow text is missing from my widget (my interaction is a dialogue), and I’m still able to walk around with my widget displayed which I shouldn’t be able to do.

If I assign my button for interaction to another button, then this work fine somehow, so I guess trouble happens inside my player controller with this left button click.

I did not really understand what you were advising me to do, sorry, I’m a real newbie, and yes, I’m trying to do all my game in blueprint (well, I just don’t know how to code). This is the tutorial I followed to make my dialogues if someone happened to have followed the same one: Unreal Engine 4 - Dialog System Tutorial (1/3) - YouTube (It’s a really good one). But basically, he launches his interactions with just one random button.

So, if anyone passing by has an idea, I could use a hand! :slight_smile:

If anyone has a similar issue, here is how I solved this (this is probably a mess and there are better ways to do this).
My problem was that I had to close the gate opened with my input (while mouse button down do…). By the way, I closed it dragging a line across all my code, if someone knows how to do this properly, I’m all ears! :smiley:
The following is situated inside of my character controller.

instead of the big line, just plug a custom event in there, and call it at the other end :slight_smile:

Yes! Thank you! This sounds like a fine solution. By the way, Zof really looks beautiful :slight_smile: