#Correcting the Weapon Rotation - issues if no linetrace hit

2 messages · Page 1 of 1 (latest)

reef beacon
#

If a linetrace does not hit, the hit target vector is (0,0,0) - this makes the weapon rotation correction react weird by rotating towards the center of the world. Sorry having posted a lot of stuff before with a working solution, but too complex. The solution for this is much simpler.

In UCombatComponent::TraceUnderCrosshairs(), add the following check after the linetrace:

GetWorld()->LineTraceSingleByChannel(TraceHitResult, Start, End, ECollisionChannel::ECC_Visibility);

// make sure, that the impact point is not (0,0,0) - set to linetrace end point
if (!TraceHitResult.bBlockingHit) TraceHitResult.ImpactPoint = End;```

And now, I believe to remember, that stuff had been in there, but Stephen did remove some whole codeblock - when I already wondered about possible side effects.

Edit: I found the place where this happened: it is in video "Replicating The Hit Target" at 07:23, where that whole codeblock does get removed.

BTW:  it looks like code can be simpler (at least for now, no need for a complete transform, we only need the location) 

```c++
// There's no need to get the full transform as done in course, location is sufficient for now
FVector RightHandLocation = EquippedWeapon->GetWeaponMesh()->GetSocketLocation(FName("hand_r"));
// we need to get the opposite direction, so do not simply pass in the Hit Target!
RightHandRotation = UKismetMathLibrary::FindLookAtRotation(RightHandLocation, RightHandLocation + (RightHandLocation - CombatHitTarget));```
reef beacon
#

Got my problems with bad looking corrections also solved now: debug lines are really helpful, and could even go into gameplay, Eject with F8 and adjust the right hand socket to bring the debug lines to match .
Apart from Stephens debug lines from muzzleflash, using these which help a lot during adjustment:

RightHandRotation = UKismetMathLibrary::FindLookAtRotation(RightHandLocation, RightHandLocation + (RightHandLocation - CombatHitTarget));
DrawDebugLine(GetWorld(), RightHandLocation, RightHandLocation + (RightHandLocation - CombatHitTarget), FColor::Blue, false, -1.f, 0, 3.f);
DrawDebugSphere(GetWorld(), RightHandLocation + (RightHandLocation - CombatHitTarget), 25.0f, 16, FColor::Blue, false, -1.f, 0, 3.f);
// yes and these as well
DrawDebugLine(GetWorld(), RightHandLocation, CombatHitTarget, FColor::Yellow, false, -1.f, 0, 3.f);
DrawDebugSphere(GetWorld(), CombatHitTarget, 25.0f, 12, FColor::Yellow, false, -1.f, 0, 3.f);
        
DrawDebugSphere(GetWorld(), RightHandLocation, 5.0f, 12, FColor::Green, false, -1.f, 0, 3.f);```

Learning so much here, really a fantastic course 💛