Announcement

Collapse
No announcement yet.

SphereSweepTrace getting area of hit.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    SphereSweepTrace getting area of hit.

    I'm doing 3rd person combat. I need to figure out what side a weapon has hit the enemy (front, back, left or right). I'm doing this by getting the angle between the other character's forward vector and the impact normal of the hitresult. I'm using SweepSingleByChannel with a sphere to get the hit result. I do something like this.

    Code:
    FVector HitActorDirection = HitResult.GetActor()->GetMesh()->GetForwardVector();
    FVector ImpactNormal = HitResult.ImpactNormal;
    
    float angle = FMath::RadiansToDegrees(acosf(FVector::DotProduct(HitActorDirection, -ImpactNormal)));
    UE_LOG(LogTemp, Warning, TEXT("angle is %f"), angle);
    But for some reason, the angles don't make sense. They are all between -90 and 70ish. I'm just trying to figure out if I'm hitting the enemy from the front or behind. How can I get the side of where I hit?
    I negated the impact normal because I read that the normal faces the center of the sphere so I have to face it the opposite direction. I tried not negating it and the angle result made less sense..
    Last edited by Ninja_K; 10-30-2017, 03:07 AM.

    #2
    Nvm, I found a much easier solution. I could just get both actor's forward vector and use that angle to determine where the attack is coming from. Can't believe I didn't think of it earlier lol.
    Here's the solution if anyone want to use this in the future.

    Code:
    ACharacter *HitActorClass = Cast<ACharacter>(HitResult.GetActor());
    
    FVector HitActorDirection = HitActorClass->GetMesh()->GetForwardVector();
    FVector OwnerActorDirection = GetOwner()->GetMesh()->GetForwardVector();
    
    float angle = FMath::RadiansToDegrees(acosf(FVector::DotProduct(HitActorDirection, OwnerActorDirection)));
    UE_LOG(LogTemp, Warning, TEXT("angle is %f"), angle);

    Comment

    Working...
    X