So I had Terrain, but it was just a big box without actual terrain yet. After replacing terrain with BSP, it’s working fine.
So what I’m doing is getting hit result under cursor with each left mouse button clicked, and sending result to character’s function which just rotate him and attack in that direction.
What I observed at first, was that sometimes character didn’t rotate, so I changed" if(HitResult.bBlockingHit)" to if(true). Now it was sometimes rotating to wrong (always the same) rotation instead of doing nothing. I couldn’t find any logical reason for me, so I changed Terrain to BSP, and now it’s working great! So I think that HitResultUnderCursor is not always working with Terrain.
Function for mouse left button:
void AAN_PlayerController::OnBasicAttackPressed()
{
FHitResult HitResult;
GetHitResultUnderCursor(ECC_Visibility, false, HitResult);
if (HitResult.bBlockingHit)
{
if (AAN_Character * Character = Cast<AAN_Character>(GetPawn()))
{
// Calculate direction
FVector HitLocation = FVector(HitResult.Location.X, HitResult.Location.Y, 0.0f);
FVector CharacterLocation = FVector(Character->GetActorLocation().X, Character->GetActorLocation().Y, 0.0f);
FVector Direction = HitLocation - Character->GetActorLocation();
Direction.Normalize();
// BasicAttack in direction of mouse
Character->BasicAttack(Direction);
}
}
}
UE 4.9.2