Hello everyone, help me implement tracing from the muzzle in file
LyraGameplayAbility_RangedWeapon.cpp
...
FTransform ULyraGameplayAbility_RangedWeapon::GetTargetingTransform(APawn* SourcePawn, ELyraAbilityTargetingSource Source) const
{
check(SourcePawn);
AController* SourcePawnController = SourcePawn->GetController();
ULyraWeaponStateComponent* WeaponStateComponent = (SourcePawnController != nullptr) ? SourcePawnController->FindComponentByClass<ULyraWeaponStateComponent>() : nullptr;
// The caller should determine the transform without calling this if the mode is custom!
check(Source != ELyraAbilityTargetingSource::Custom);
const FVector ActorLoc = SourcePawn->GetActorLocation();
FQuat AimQuat = SourcePawn->GetActorQuat();
AController* Controller = SourcePawn->Controller;
FVector SourceLoc;
double FocalDistance = 1024.0f;
FVector FocalLoc;
FVector CamLoc;
FRotator CamRot;
bool bFoundFocus = false;
if ((Controller != nullptr) && ((Source == ELyraAbilityTargetingSource::CameraTowardsFocus) || (Source == ELyraAbilityTargetingSource::PawnTowardsFocus) || (Source == ELyraAbilityTargetingSource::WeaponTowardsFocus)))
{
// Get camera position for later
bFoundFocus = true;
APlayerController* PC = Cast<APlayerController>(Controller);
if (PC != nullptr)
{
PC->GetPlayerViewPoint(/*out*/ CamLoc, /*out*/ CamRot);
}
else
{
SourceLoc = GetWeaponTargetingSourceLocation();
CamLoc = SourceLoc;
CamRot = Controller->GetControlRotation();
}
// Determine initial focal point to
FVector AimDir = CamRot.Vector().GetSafeNormal();
FocalLoc = CamLoc + (AimDir * FocalDistance);
// Move the start and focal point up in front of pawn
if (PC)
{
const FVector WeaponLoc = GetWeaponTargetingSourceLocation();
CamLoc = FocalLoc + (((WeaponLoc - FocalLoc) | AimDir) * AimDir);
FocalLoc = CamLoc + (AimDir * FocalDistance);
}
//Move the start to be the HeadPosition of the AI
else if (AAIController* AIController = Cast<AAIController>(Controller))
{
CamLoc = SourcePawn->GetActorLocation() + FVector(0, 0, SourcePawn->BaseEyeHeight);
}
if (Source == ELyraAbilityTargetingSource::CameraTowardsFocus)
{
// If we're camera -> focus then we're done
return FTransform(CamRot, CamLoc);
}
}
if ((Source == ELyraAbilityTargetingSource::WeaponForward) || (Source == ELyraAbilityTargetingSource::WeaponTowardsFocus))
{
SourceLoc = GetWeaponTargetingSourceLocation();
}
else
{
// Either we want the pawn's location, or we failed to find a camera
SourceLoc = ActorLoc;
}
if (bFoundFocus && ((Source == ELyraAbilityTargetingSource::PawnTowardsFocus) || (Source == ELyraAbilityTargetingSource::WeaponTowardsFocus)))
{
// Return a rotator pointing at the focal point from the source
return FTransform((FocalLoc - SourceLoc).Rotation(), SourceLoc);
}
// If we got here, either we don't have a camera or we don't want to use it, either way go forward
return FTransform(AimQuat, SourceLoc);
}
...
All if’s start method GetWeaponTargetingSourceLocation()
FVector ULyraGameplayAbility_RangedWeapon::GetWeaponTargetingSourceLocation() const
{
// Use Pawn's location as a base
APawn* const AvatarPawn = Cast<APawn>(GetAvatarActorFromActorInfo());
check(AvatarPawn);
const FVector SourceLoc = AvatarPawn->GetActorLocation();
const FQuat SourceRot = AvatarPawn->GetActorQuat();
FVector TargetingSourceLocation = SourceLoc;
//@TODO: Add an offset from the weapon instance and adjust based on pawn crouch/aiming/etc...
return TargetingSourceLocation;
}
It contains GetActorLocation() and GetActorQuat() from AvatarPawn.
How to get the socket of the current weapon and its position and rotation?