https://forums.unrealengine.com/t/top-down-view-left-button-move-and-interact/153897/4
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()
{
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.
댓글
댓글 쓰기