Skip to content

Instantly share code, notes, and snippets.

@vsrc
Forked from wxvyjb/IKAnimInstance_TrueFP.cpp
Created June 14, 2026 23:59
Show Gist options
  • Select an option

  • Save vsrc/c8a7098e237d59e4bb1936642213a147 to your computer and use it in GitHub Desktop.

Select an option

Save vsrc/c8a7098e237d59e4bb1936642213a147 to your computer and use it in GitHub Desktop.
Unreal Engine - Procedural Aiming AnimInstance
#include "Character/Animation/IKAnimInstance_TrueFP.h"
#include "AbilitySystemComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "Curves/CurveVector.h"
#include "Framework/EquipmentComponent.h"
#include "Framework/EquippedItemActor.h"
#include "Framework/Weapon/WeaponActor.h"
#include "Framework/Weapon/WeaponInstance.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "KismetAnimationLibrary.h"
#include "Player/XPlayerCharacter.h"
#include "Player/XPlayerCharacter_TrueFP.h"
#include "Player/XPlayerState.h"
UIKAnimInstance_TrueFP::UIKAnimInstance_TrueFP()
{
bFirstPerson = false;
bInterpAiming = false;
bIsAiming = false;
bInterpRelativeToHand = false;
bMixamoSkeleton = false;
LeftHandIKSocketName = FName("LeftHandGrip_1P");
LeftHandBoneName = FName("hand_l");
RightHandBoneName = FName("hand_r");
LeftForearmBoneName = FName("lowerarm_l");
RightForearmBoneName = FName("lowerarm_r");
LeftForearmTwistBoneName = FName("lowerarm_twist_01_l");
RightForearmTwistBoneName = FName("lowerarm_twist_01_r");
DefaultWeaponSightSocketName = FName("OpticAimpoint");
CurrentSightSocketName = DefaultWeaponSightSocketName;
CameraSocketName = FName("Camera");
SightSocketNames.Add(DefaultWeaponSightSocketName);
LookIKBoneCount = 4;
SpineIKRotation.Pitch = 0.0f;
RightHandIKEffectorLocation = FVector(-40.0f, -3.5f, 36.0f);
ReloadingAlpha = 0.0f;
}
void UIKAnimInstance_TrueFP::NativeBeginPlay()
{
Super::NativeBeginPlay();
if (AXPlayerCharacter* Char = Cast<AXPlayerCharacter>(TryGetPawnOwner()))
{
Character = Char;
OldRotation = Character->GetControlRotation();
BindCharacterDelegates();
SyncEquippedActorReferences();
}
if (GetWorld() && LookIKBoneCount > 0)
{
SetSpineIKRotation();
FTimerHandle RefreshIKTimerHandle;
GetWorld()->GetTimerManager().SetTimer(RefreshIKTimerHandle, this, &UIKAnimInstance_TrueFP::RefreshSightAndHandTransforms, 0.3f, false);
}
}
void UIKAnimInstance_TrueFP::NativeUpdateAnimation(float DeltaSeconds)
{
Super::NativeUpdateAnimation(DeltaSeconds);
if (!Character)
{
Character = Cast<AXPlayerCharacter>(TryGetPawnOwner());
if (Character)
{
OldRotation = Character->GetControlRotation();
BindCharacterDelegates();
SyncEquippedActorReferences();
}
}
if (GetWorld() && LookIKBoneCount > 0)
{
SetSpineIKRotation();
}
if (!Character)
{
return;
}
AWeaponActor* EquippedWeapon = Character->GetEquippedWeapon();
if (!EquippedWeapon)
{
if (const UEquipmentComponent* EquipmentComponent = Character->GetEquipmentComponent())
{
EquippedWeapon = EquipmentComponent->EquippedWeaponActor;
}
}
if (EquippedWeapon != CurrentWeapon)
{
SetCharacterWeapon(EquippedWeapon);
}
bIsLocallyControlled = Character->IsLocallyControlled();
bIsFalling = Character->GetMovementComponent() ? Character->GetMovementComponent()->IsFalling() : false;
bIsKnockedDown = CheckIfKnockedDown();
bIsSprinting = Character->IsSprinting();
bIsCrouching = Character->IsCrouching();
bIsAiming = Character->IsAiming();
bWeaponEquipped = IsValid(CurrentWeapon);
if (!bWeaponEquipped)
{
LeftHandIKAlpha = 0.0f;
}
Speed = Character->GetVelocity().Size();
bShouldMove = Speed > 3.0f;
Direction = UKismetAnimationLibrary::CalculateDirection(Character->GetVelocity(), Character->GetActorRotation());
AimOffsetPitch = UKismetMathLibrary::ClampAngle(Character->GetBaseAimRotation().Pitch, -90.0f, 90.0f);
UpdateLean(DeltaSeconds);
UpdateWristTwistCorrection();
SetTurnSway(DeltaSeconds);
SetMoveVectorCurve(DeltaSeconds);
if (bInterpAiming)
{
InterpAimingAlpha(DeltaSeconds);
}
if (bInterpRelativeToHand)
{
InterpRelativeToHand(DeltaSeconds);
}
if (bWeaponEquipped)
{
SetLeftHandIK();
if (!Character->IsReloading())
{
LeftHandIKAlpha = 1.0f;
}
if (ArmedAimOffset && ArmedHipOffset)
{
CurrentAimOffset = bIsAiming ? ArmedAimOffset : ArmedHipOffset;
}
}
else if (UnarmedOffset)
{
CurrentAimOffset = UnarmedOffset;
}
}
void UIKAnimInstance_TrueFP::NativeUninitializeAnimation()
{
UnbindWeaponVisualDelegates();
UnbindCharacterDelegates();
Super::NativeUninitializeAnimation();
}
void UIKAnimInstance_TrueFP::SetCharacerWeapon(AWeaponActor* InWeapon)
{
SetCharacterWeapon(InWeapon);
}
void UIKAnimInstance_TrueFP::SetCharacterWeapon(AWeaponActor* InWeapon)
{
BindWeaponVisualDelegates(InWeapon);
CurrentWeapon = InWeapon;
CharacterWeapon = InWeapon;
RefreshWeaponData();
bWeaponEquipped = IsValid(CurrentWeapon);
if (InWeapon)
{
CurrentEquippedItemActor = InWeapon;
}
else
{
LeftHandIKAlpha = 0.0f;
}
RefreshSightAndHandTransforms();
if (InWeapon && (!Character || !Character->IsReloading()))
{
LeftHandIKAlpha = 1.0f;
}
}
void UIKAnimInstance_TrueFP::SetCurrentEquippedItemActor(AEquippedItemActor* InItemActor)
{
CurrentEquippedItemActor = InItemActor;
if (AWeaponActor* WeaponActor = Cast<AWeaponActor>(InItemActor))
{
SetCharacterWeapon(WeaponActor);
}
else
{
SetCharacterWeapon(nullptr);
}
}
void UIKAnimInstance_TrueFP::BindCharacterDelegates()
{
if (!Character)
{
return;
}
Character->OnEquippedItemActorSet.RemoveDynamic(this, &UIKAnimInstance_TrueFP::HandleEquippedItemActorSet);
Character->OnEquippedItemActorSet.AddDynamic(this, &UIKAnimInstance_TrueFP::HandleEquippedItemActorSet);
Character->OnEquippedWeaponSet.RemoveDynamic(this, &UIKAnimInstance_TrueFP::HandleEquippedWeaponSet);
Character->OnEquippedWeaponSet.AddDynamic(this, &UIKAnimInstance_TrueFP::HandleEquippedWeaponSet);
Character->OnAimUpdated.RemoveDynamic(this, &UIKAnimInstance_TrueFP::HandleAimUpdated);
Character->OnAimUpdated.AddDynamic(this, &UIKAnimInstance_TrueFP::HandleAimUpdated);
Character->OnIsReloadingUpdated.RemoveDynamic(this, &UIKAnimInstance_TrueFP::HandleReloadingUpdated);
Character->OnIsReloadingUpdated.AddDynamic(this, &UIKAnimInstance_TrueFP::HandleReloadingUpdated);
}
void UIKAnimInstance_TrueFP::UnbindCharacterDelegates()
{
if (!Character)
{
return;
}
Character->OnEquippedItemActorSet.RemoveDynamic(this, &UIKAnimInstance_TrueFP::HandleEquippedItemActorSet);
Character->OnEquippedWeaponSet.RemoveDynamic(this, &UIKAnimInstance_TrueFP::HandleEquippedWeaponSet);
Character->OnAimUpdated.RemoveDynamic(this, &UIKAnimInstance_TrueFP::HandleAimUpdated);
Character->OnIsReloadingUpdated.RemoveDynamic(this, &UIKAnimInstance_TrueFP::HandleReloadingUpdated);
}
void UIKAnimInstance_TrueFP::BindWeaponVisualDelegates(AWeaponActor* InWeapon)
{
if (BoundWeaponVisualsActor == InWeapon)
{
return;
}
UnbindWeaponVisualDelegates();
BoundWeaponVisualsActor = InWeapon;
if (BoundWeaponVisualsActor)
{
BoundWeaponVisualsActor->OnVisualsUpdated.RemoveDynamic(this, &UIKAnimInstance_TrueFP::HandleWeaponVisualsUpdated);
BoundWeaponVisualsActor->OnVisualsUpdated.AddDynamic(this, &UIKAnimInstance_TrueFP::HandleWeaponVisualsUpdated);
BoundWeaponVisualsActor->OnReloadStarted.RemoveDynamic(this, &UIKAnimInstance_TrueFP::HandleWeaponReloadStarted);
BoundWeaponVisualsActor->OnReloadStarted.AddDynamic(this, &UIKAnimInstance_TrueFP::HandleWeaponReloadStarted);
}
}
void UIKAnimInstance_TrueFP::UnbindWeaponVisualDelegates()
{
if (!BoundWeaponVisualsActor)
{
return;
}
BoundWeaponVisualsActor->OnVisualsUpdated.RemoveDynamic(this, &UIKAnimInstance_TrueFP::HandleWeaponVisualsUpdated);
BoundWeaponVisualsActor->OnReloadStarted.RemoveDynamic(this, &UIKAnimInstance_TrueFP::HandleWeaponReloadStarted);
BoundWeaponVisualsActor = nullptr;
}
void UIKAnimInstance_TrueFP::SyncEquippedActorReferences()
{
if (!Character)
{
SetCurrentEquippedItemActor(nullptr);
return;
}
CurrentEquippedItemActor = Character->CurrentEquippedItem;
SetCharacterWeapon(Character->GetEquippedWeapon());
if (!CurrentEquippedItemActor)
{
CurrentEquippedItemActor = CurrentWeapon;
}
}
void UIKAnimInstance_TrueFP::RefreshWeaponData()
{
WeaponType = EGunType::None;
AmmoFeedType = EAmmoFeedType::None;
if (CurrentWeapon)
{
if (const UWeaponInstance* WeaponInstance = CurrentWeapon->GetWeaponInstance())
{
WeaponType = WeaponInstance->GunData.GunType;
AmmoFeedType = WeaponInstance->GunData.AmmoFeedType;
}
}
}
void UIKAnimInstance_TrueFP::RefreshSightAndHandTransforms()
{
SetSightTransform();
SetRelativeHandTransform();
SetFinalHandTransform();
}
USkeletalMeshComponent* UIKAnimInstance_TrueFP::GetArmsMesh() const
{
if (const AXPlayerCharacter_TrueFP* TrueFPCharacter = Cast<AXPlayerCharacter_TrueFP>(Character))
{
return TrueFPCharacter->GetTrueFPFullBodyMesh();
}
return Character ? Character->GetFirstPersonAttachMesh() : nullptr;
}
USceneComponent* UIKAnimInstance_TrueFP::GetWeaponMesh() const
{
return CurrentWeapon ? CurrentWeapon->GetWeaponVisualComponent() : nullptr;
}
FName UIKAnimInstance_TrueFP::GetActiveSightSocketName() const
{
if (SightSocketNames.IsValidIndex(CurrentSightSocketIndex) && SightSocketNames[CurrentSightSocketIndex] != NAME_None)
{
return SightSocketNames[CurrentSightSocketIndex];
}
return CurrentSightSocketName != NAME_None ? CurrentSightSocketName : DefaultWeaponSightSocketName;
}
void UIKAnimInstance_TrueFP::SetSpineIKRotation()
{
if (!Character || LookIKBoneCount == 0)
{
return;
}
const FRotator UseRotator = Character->IsLocallyControlled()
? Character->GetControlRotation()
: Character->GetBaseAimRotation();
const FRotator CombinedRotator = UKismetMathLibrary::ComposeRotators(
UseRotator,
FRotator(180.0f, 0.0f, 0.0f)
);
SpineIKRotation = FRotator(0.0f, 0.0f, CombinedRotator.Pitch / LookIKBoneCount);
}
void UIKAnimInstance_TrueFP::SetMoveVectorCurve(float DeltaSeconds)
{
if (!Character || !MoveVectorCurve || !Character->GetCharacterMovement())
{
return;
}
FVector VelocityVector = Character->GetCharacterMovement()->Velocity;
VelocityVector.Z = 0.0f;
const float MaxSpeed = FMath::Max(Character->GetCharacterMovement()->GetMaxSpeed(), 1.0f);
const float VelocityScale = UKismetMathLibrary::NormalizeToRange(VelocityVector.Size(), (MaxSpeed / 0.3f) * -1.0f, MaxSpeed);
const FVector TargetVector = MoveVectorCurve->GetVectorValue(Character->GetGameTimeSinceCreation());
MoveSwayLocation = UKismetMathLibrary::VInterpTo(MoveSwayLocation, TargetVector, DeltaSeconds, MoveSwayInterpSpeed) * VelocityScale * MoveSwayFinalScale;
}
void UIKAnimInstance_TrueFP::SetTurnSway(float DeltaSeconds)
{
if (!Character)
{
return;
}
const FRotator CurrentRotation = Character->GetControlRotation();
const FRotator TargetRotation = CurrentRotation - OldRotation;
const FRotator InterpedTurnRotation = UKismetMathLibrary::RInterpTo(TurnRotation, TargetRotation, DeltaSeconds, TurnSwayInterpSpeed);
const float EffectiveSwayScale = FMath::Clamp(FMath::Lerp(1.0f, AimingTurnSwayScale, AimingAlpha), 0.0f, 1.0f);
TurnRotation = InterpedTurnRotation * EffectiveSwayScale;
TurnRotation.Roll = InterpedTurnRotation.Yaw * -2.0f * TurnSwayYawRollScale * EffectiveSwayScale;
OldRotation = CurrentRotation;
}
void UIKAnimInstance_TrueFP::UpdateLean(float DeltaSeconds)
{
if (!Character)
{
SmoothedLeanAngle = 0.0f;
SmoothedLeanRotator = FRotator::ZeroRotator;
TargetLeanAngle = 0.0f;
LeanStartAngle = 0.0f;
LeanElapsedTime = 0.0f;
return;
}
const float NewTargetLeanAngle = Character->GetLeanAngle();
if (!FMath::IsNearlyEqual(TargetLeanAngle, NewTargetLeanAngle, 0.01f))
{
TargetLeanAngle = NewTargetLeanAngle;
LeanStartAngle = SmoothedLeanAngle;
LeanElapsedTime = 0.0f;
}
if (TimeToLean <= 0.0f || FMath::IsNearlyEqual(SmoothedLeanAngle, TargetLeanAngle, 0.01f))
{
SmoothedLeanAngle = TargetLeanAngle;
}
else
{
LeanElapsedTime += DeltaSeconds;
const float LeanAlpha = FMath::Clamp(LeanElapsedTime / TimeToLean, 0.0f, 1.0f);
SmoothedLeanAngle = FMath::Lerp(LeanStartAngle, TargetLeanAngle, LeanAlpha);
}
SmoothedLeanRotator = FRotator(SmoothedLeanAngle, 0.0f, 0.0f);
}
void UIKAnimInstance_TrueFP::UpdateWristTwistCorrection()
{
USkeletalMeshComponent* ArmsMesh = GetArmsMesh();
if (!bEnableWristTwistCorrection || !ArmsMesh)
{
LeftWristTwistValue = 0.0f;
RightWristTwistValue = 0.0f;
LeftForearmTwistRotation = FRotator::ZeroRotator;
RightForearmTwistRotation = FRotator::ZeroRotator;
return;
}
LeftWristTwistValue = CalculateWristTwistValue(ArmsMesh, LeftHandBoneName, LeftForearmBoneName, LeftWristTwistScale);
RightWristTwistValue = CalculateWristTwistValue(ArmsMesh, RightHandBoneName, RightForearmBoneName, RightWristTwistScale);
LeftForearmTwistRotation = MakeWristTwistRotator(LeftWristTwistValue);
RightForearmTwistRotation = MakeWristTwistRotator(RightWristTwistValue);
}
float UIKAnimInstance_TrueFP::CalculateWristTwistValue(USkeletalMeshComponent* ArmsMesh, FName HandBoneName, FName ForearmBoneName, float TwistScale) const
{
if (!ArmsMesh || HandBoneName == NAME_None || ForearmBoneName == NAME_None)
{
return 0.0f;
}
if (ArmsMesh->GetBoneIndex(HandBoneName) == INDEX_NONE || ArmsMesh->GetBoneIndex(ForearmBoneName) == INDEX_NONE)
{
return 0.0f;
}
const FTransform HandTransform = ArmsMesh->GetSocketTransform(HandBoneName, RTS_Component);
const FTransform ForearmTransform = ArmsMesh->GetSocketTransform(ForearmBoneName, RTS_Component);
const FRotator LocalWristRotation = UKismetMathLibrary::MakeRelativeTransform(HandTransform, ForearmTransform).Rotator();
float TwistAngle = 0.0f;
switch (WristTwistAxis)
{
case EWristTwistAxis::Pitch:
TwistAngle = LocalWristRotation.Pitch;
break;
case EWristTwistAxis::Yaw:
TwistAngle = LocalWristRotation.Yaw;
break;
case EWristTwistAxis::Roll:
default:
TwistAngle = LocalWristRotation.Roll;
break;
}
const float ScaledTwist = FRotator::NormalizeAxis(TwistAngle) * TwistScale;
return FMath::Clamp(ScaledTwist, -MaxWristTwistAngle, MaxWristTwistAngle);
}
FRotator UIKAnimInstance_TrueFP::MakeWristTwistRotator(float TwistValue) const
{
FRotator TwistRotation = FRotator::ZeroRotator;
switch (WristTwistAxis)
{
case EWristTwistAxis::Pitch:
TwistRotation.Pitch = TwistValue;
break;
case EWristTwistAxis::Yaw:
TwistRotation.Yaw = TwistValue;
break;
case EWristTwistAxis::Roll:
default:
TwistRotation.Roll = TwistValue;
break;
}
return TwistRotation;
}
void UIKAnimInstance_TrueFP::CycleSights()
{
if (SightSocketNames.Num() > 0)
{
CurrentSightSocketIndex = (CurrentSightSocketIndex + 1) % SightSocketNames.Num();
CurrentSightSocketName = SightSocketNames[CurrentSightSocketIndex];
}
SetSightTransform();
SetFinalHandTransform();
bInterpRelativeToHand = true;
}
void UIKAnimInstance_TrueFP::SetIsAiming(bool bNewAiming)
{
if (bIsAiming != bNewAiming)
{
bIsAiming = bNewAiming;
bInterpAiming = true;
}
}
bool UIKAnimInstance_TrueFP::CheckIfKnockedDown()
{
if (Character)
{
if (AXPlayerState* PS = Cast<AXPlayerState>(Character->GetPlayerState()))
{
return PS->GetAbilitySystemComponent()
&& PS->GetAbilitySystemComponent()->HasMatchingGameplayTag(Character->GetKnockedDownTag())
&& !PS->GetAbilitySystemComponent()->HasMatchingGameplayTag(Character->GetDeadTag());
}
}
return false;
}
void UIKAnimInstance_TrueFP::SetLeftHandIK()
{
USkeletalMeshComponent* ArmsMesh = GetArmsMesh();
USceneComponent* WeaponMesh = GetWeaponMesh();
if (!ArmsMesh || !WeaponMesh || LeftHandIKSocketName == NAME_None || !WeaponMesh->DoesSocketExist(LeftHandIKSocketName))
{
return;
}
const FName HandBoneName = RightHandBoneName != NAME_None ? RightHandBoneName : FName("hand_r");
const FTransform WeaponSocketTransform = WeaponMesh->GetSocketTransform(LeftHandIKSocketName, RTS_World);
FVector BoneSpaceLocation = FVector::ZeroVector;
FRotator BoneSpaceRotation = FRotator::ZeroRotator;
ArmsMesh->TransformToBoneSpace(
HandBoneName,
WeaponSocketTransform.GetLocation(),
WeaponSocketTransform.Rotator(),
BoneSpaceLocation,
BoneSpaceRotation
);
LeftHandTransform.SetLocation(BoneSpaceLocation);
LeftHandTransform.SetRotation(BoneSpaceRotation.Quaternion());
LeftHandTransform.SetScale3D(FVector::OneVector);
}
void UIKAnimInstance_TrueFP::InterpRelativeToHand(float DeltaSeconds)
{
RelativeHandTransform = UKismetMathLibrary::TInterpTo(RelativeHandTransform, FinalHandTransform, DeltaSeconds, AimingInterpSpeed);
HandRelativeToSightTransform = UKismetMathLibrary::TInterpTo(HandRelativeToSightTransform, FinalHandRelativeToSightTransform, DeltaSeconds, AimingInterpSpeed);
if (RelativeHandTransform.Equals(FinalHandTransform) && HandRelativeToSightTransform.Equals(FinalHandRelativeToSightTransform))
{
bInterpRelativeToHand = false;
}
}
void UIKAnimInstance_TrueFP::InterpAimingAlpha(float DeltaSeconds)
{
AimingAlpha = UKismetMathLibrary::FInterpTo(AimingAlpha, static_cast<float>(bIsAiming), DeltaSeconds, AimingInterpSpeed);
if ((bIsAiming && AimingAlpha >= 1.0f) || (!bIsAiming && AimingAlpha <= 0.0f))
{
bInterpAiming = false;
}
}
void UIKAnimInstance_TrueFP::HandleEquippedItemActorSet(AActor* UpdatedActor)
{
SetCurrentEquippedItemActor(Cast<AEquippedItemActor>(UpdatedActor));
}
void UIKAnimInstance_TrueFP::HandleEquippedWeaponSet(AActor* UpdatedActor)
{
SetCharacterWeapon(Cast<AWeaponActor>(UpdatedActor));
}
void UIKAnimInstance_TrueFP::HandleAimUpdated(bool bNewAiming)
{
SetIsAiming(bNewAiming);
}
void UIKAnimInstance_TrueFP::HandleReloadingUpdated(bool bNewReloading)
{
RefreshSightAndHandTransforms();
LeftHandIKAlpha = bNewReloading ? 0.0f : 1.0f;
ReloadingAlpha = bNewReloading ? 1.0f : 0.0f;
}
void UIKAnimInstance_TrueFP::HandleWeaponVisualsUpdated()
{
RefreshWeaponData();
RefreshSightAndHandTransforms();
}
void UIKAnimInstance_TrueFP::HandleWeaponReloadStarted()
{
RefreshWeaponData();
OnWeaponReloadStarted(CurrentWeapon, WeaponType, AmmoFeedType);
}
void UIKAnimInstance_TrueFP::SetSightTransform()
{
USkeletalMeshComponent* ArmsMesh = GetArmsMesh();
USceneComponent* WeaponMesh = GetWeaponMesh();
if (!Character || !ArmsMesh || !WeaponMesh)
{
return;
}
const AXPlayerCharacter_TrueFP* TrueFPCharacter = Cast<AXPlayerCharacter_TrueFP>(Character);
FName EffectiveCameraSocketName = CameraSocketName != NAME_None ? CameraSocketName : FName("Camera");
if (TrueFPCharacter)
{
if (TrueFPCharacter->TrueFPCameraSocketName != NAME_None)
{
EffectiveCameraSocketName = TrueFPCharacter->TrueFPCameraSocketName;
}
}
if (!ArmsMesh->DoesSocketExist(EffectiveCameraSocketName) && ArmsMesh->GetBoneIndex(EffectiveCameraSocketName) == INDEX_NONE)
{
return;
}
const FTransform CameraSocketTransform = ArmsMesh->GetSocketTransform(EffectiveCameraSocketName, RTS_ParentBoneSpace);
const FTransform SightSocketTransform = WeaponMesh->GetSocketTransform(GetActiveSightSocketName());
const FName HandBoneName = RightHandBoneName != NAME_None ? RightHandBoneName : FName("hand_r");
const FTransform HandTransform = ArmsMesh->GetSocketTransform(HandBoneName);
const FTransform HandRelativeToSight = UKismetMathLibrary::MakeRelativeTransform(HandTransform, SightSocketTransform);
const float DistanceFromCameraCompensated = WeaponHandForwardOffset - HandRelativeToSight.GetLocation().X;
const FVector SightLocation = CameraSocketTransform.GetLocation()
+ (CameraSocketTransform.GetRotation().GetForwardVector() * DistanceFromCameraCompensated);
SightTransform.SetLocation(SightLocation);
SightTransform.SetRotation(CameraSocketTransform.GetRotation());
SightTransform.SetScale3D(FVector(DistanceFromCameraCompensated, 1.0f, 1.0f));
}
void UIKAnimInstance_TrueFP::SetRelativeHandTransform()
{
USkeletalMeshComponent* ArmsMesh = GetArmsMesh();
USceneComponent* WeaponMesh = GetWeaponMesh();
if (!ArmsMesh || !WeaponMesh)
{
return;
}
const FTransform SightSocketTransform = WeaponMesh->GetSocketTransform(GetActiveSightSocketName());
const FName HandBoneName = RightHandBoneName != NAME_None ? RightHandBoneName : FName("hand_r");
const FTransform HandSocketTransform = ArmsMesh->GetSocketTransform(HandBoneName);
RelativeHandTransform = UKismetMathLibrary::MakeRelativeTransform(SightSocketTransform, HandSocketTransform);
HandRelativeToSightTransform = UKismetMathLibrary::MakeRelativeTransform(HandSocketTransform, SightSocketTransform);
}
void UIKAnimInstance_TrueFP::SetFinalHandTransform()
{
USkeletalMeshComponent* ArmsMesh = GetArmsMesh();
USceneComponent* WeaponMesh = GetWeaponMesh();
if (!ArmsMesh || !WeaponMesh)
{
return;
}
const FTransform SightSocketTransform = WeaponMesh->GetSocketTransform(GetActiveSightSocketName());
const FName HandBoneName = RightHandBoneName != NAME_None ? RightHandBoneName : FName("hand_r");
const FTransform HandSocketTransform = ArmsMesh->GetSocketTransform(HandBoneName);
FinalHandTransform = UKismetMathLibrary::MakeRelativeTransform(SightSocketTransform, HandSocketTransform);
FinalHandRelativeToSightTransform = UKismetMathLibrary::MakeRelativeTransform(HandSocketTransform, SightSocketTransform);
}
void UIKAnimInstance_TrueFP::Reload()
{
LeftHandIKAlpha = FMath::IsNearlyEqual(LeftHandIKAlpha, 1.0f) ? 0.0f : 1.0f;
ReloadingAlpha = 1.0f;
}
void UIKAnimInstance_TrueFP::ReloadFinished()
{
LeftHandIKAlpha = 1.0f;
ReloadingAlpha = 0.0f;
}
/**
* Procedural based AnimInstance meant for a true first person controller. Needs a weapon to provide valid sight transforms.
*/
UCLASS()
class YOURGAME_API UIKAnimInstance_TrueFP : public UAnimInstance
{
GENERATED_BODY()
public:
UIKAnimInstance_TrueFP();
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
TObjectPtr<AXPlayerCharacter> Character = nullptr;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Item")
TObjectPtr<AEquippedItemActor> CurrentEquippedItemActor = nullptr;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Weapon")
TObjectPtr<AWeaponActor> CurrentWeapon = nullptr;
UPROPERTY(Transient)
TObjectPtr<AWeaponActor> BoundWeaponVisualsActor = nullptr;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Weapon")
TObjectPtr<AWeaponActor> CharacterWeapon = nullptr;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Weapon")
EGunType WeaponType = EGunType::None;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Weapon")
EAmmoFeedType AmmoFeedType = EAmmoFeedType::None;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "TurnSway")
FRotator TurnRotation;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "TurnSway")
FRotator OldRotation;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "TurnSway")
float TurnSwayInterpSpeed = 5.0f;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "TurnSway", meta = (ClampMin = "0.0", ClampMax = "1.0", UIMin = "0.0", UIMax = "1.0"))
float AimingTurnSwayScale = 0.35f;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "TurnSway", meta = (ClampMin = "0.0", ClampMax = "1.0", UIMin = "0.0", UIMax = "1.0"))
float TurnSwayYawRollScale = 1.0f;
// Seconds it takes the animation pose to reach the character's replicated lean target.
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Lean", meta = (ClampMin = "0.0", UIMin = "0.0"))
float TimeToLean = 0.18f;
// Current smoothed lean angle for animation. Negative leans left, positive leans right.
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Lean")
float SmoothedLeanAngle = 0.0f;
// Rotator built from SmoothedLeanAngle for AnimBP Transform/Modify Bone nodes.
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Lean")
FRotator SmoothedLeanRotator = FRotator::ZeroRotator;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "MoveSway")
float MoveSwayInterpSpeed = 1.8f;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "MoveSway")
FVector MoveSwayLocation;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "MoveSway")
float MoveSwayFinalScale = 1.0f;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "MoveSway")
TObjectPtr<UCurveVector> MoveVectorCurve = nullptr;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "HandIK")
FTransform RelativeHandTransform;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "HandIK")
FTransform HandRelativeToSightTransform;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|Weapon")
FTransform WeaponGripTransform;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "HandIK|Weapon")
FTransform SightTransform;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|Weapon")
FName CameraSocketName;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "HandIK")
FTransform FinalHandTransform;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "HandIK")
FTransform FinalHandRelativeToSightTransform;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "HandIK")
FTransform LeftHandTransform;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
TObjectPtr<UBlendSpace> CurrentAimOffset = nullptr;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
TObjectPtr<UBlendSpace> UnarmedOffset = nullptr;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
TObjectPtr<UBlendSpace> ArmedHipOffset = nullptr;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
TObjectPtr<UBlendSpace> ArmedAimOffset = nullptr;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
bool bFirstPerson;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
bool bMixamoSkeleton;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
FName DefaultWeaponSightSocketName;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|Weapon")
TArray<FName> SightSocketNames;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "HandIK|Weapon")
int32 CurrentSightSocketIndex = 0;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|Weapon")
FName CurrentSightSocketName;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK")
FVector RightHandIKEffectorLocation;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
FName LeftHandBoneName;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
FName RightHandBoneName;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
FName LeftHandIKSocketName;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|WristTwist")
bool bEnableWristTwistCorrection = true;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|WristTwist")
EWristTwistAxis WristTwistAxis = EWristTwistAxis::Roll;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|WristTwist")
FName LeftForearmBoneName;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|WristTwist")
FName RightForearmBoneName;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|WristTwist")
FName LeftForearmTwistBoneName;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|WristTwist")
FName RightForearmTwistBoneName;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|WristTwist", meta = (ClampMin = "-2.0", ClampMax = "2.0", UIMin = "-1.0", UIMax = "1.0"))
float LeftWristTwistScale = 0.5f;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|WristTwist", meta = (ClampMin = "-2.0", ClampMax = "2.0", UIMin = "-1.0", UIMax = "1.0"))
float RightWristTwistScale = 0.5f;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "HandIK|WristTwist", meta = (ClampMin = "0.0", UIMin = "0.0"))
float MaxWristTwistAngle = 90.0f;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "HandIK|WristTwist")
float LeftWristTwistValue = 0.0f;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "HandIK|WristTwist")
float RightWristTwistValue = 0.0f;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "HandIK|WristTwist")
FRotator LeftForearmTwistRotation = FRotator::ZeroRotator;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "HandIK|WristTwist")
FRotator RightForearmTwistRotation = FRotator::ZeroRotator;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
uint8 LookIKBoneCount;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
FRotator SpineIKRotation;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
float LeftHandIKAlpha = 1.0f;
UPROPERTY(BlueprintReadWrite, VisibleDefaultsOnly)
float AimingInterpSpeed = 10.0f;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Character")
float AimingAlpha = 0.0f;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Weapon")
float ReloadingAlpha = 0.0f;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
float Direction = 0.0f;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
float Speed = 0.0f;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bShouldMove = false;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
float AimOffsetPitch = 0.0f;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Character")
float WeaponHandForwardOffset = 30.0f;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bIsLocallyControlled;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bInventoryOpen;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bWeaponEquipped;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bIsKnockedDown;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bInterpRelativeToHand;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bInterpAiming;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bIsAiming;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bIsSprinting;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bIsCrouching;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bIsSliding;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bIsVaulting;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bIsHanging;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Character")
bool bIsFalling;
virtual void NativeBeginPlay() override;
virtual void NativeUpdateAnimation(float DeltaSeconds) override;
virtual void NativeUninitializeAnimation() override;
public:
UFUNCTION(BlueprintCallable)
void SetCharacerWeapon(AWeaponActor* InWeapon);
UFUNCTION(BlueprintCallable)
void SetCharacterWeapon(AWeaponActor* InWeapon);
UFUNCTION(BlueprintCallable)
void SetCurrentEquippedItemActor(AEquippedItemActor* InItemActor);
UFUNCTION(BlueprintCallable)
void CycleSights();
UFUNCTION(BlueprintCallable)
void SetIsAiming(bool bNewAiming);
protected:
bool CheckIfKnockedDown();
void BindCharacterDelegates();
void UnbindCharacterDelegates();
void BindWeaponVisualDelegates(AWeaponActor* InWeapon);
void UnbindWeaponVisualDelegates();
void SyncEquippedActorReferences();
void RefreshWeaponData();
void RefreshSightAndHandTransforms();
USkeletalMeshComponent* GetArmsMesh() const;
USceneComponent* GetWeaponMesh() const;
FName GetActiveSightSocketName() const;
void SetSpineIKRotation();
void SetMoveVectorCurve(float DeltaSeconds);
void SetTurnSway(float DeltaSeconds);
void UpdateLean(float DeltaSeconds);
void UpdateWristTwistCorrection();
float CalculateWristTwistValue(USkeletalMeshComponent* ArmsMesh, FName HandBoneName, FName ForearmBoneName, float TwistScale) const;
FRotator MakeWristTwistRotator(float TwistValue) const;
void SetLeftHandIK();
void InterpRelativeToHand(float DeltaSeconds);
void InterpAimingAlpha(float DeltaSeconds);
float TargetLeanAngle = 0.0f;
float LeanStartAngle = 0.0f;
float LeanElapsedTime = 0.0f;
UFUNCTION()
void HandleEquippedItemActorSet(AActor* UpdatedActor);
UFUNCTION()
void HandleEquippedWeaponSet(AActor* UpdatedActor);
UFUNCTION()
void HandleAimUpdated(bool bNewAiming);
UFUNCTION()
void HandleReloadingUpdated(bool bNewReloading);
UFUNCTION()
void HandleWeaponVisualsUpdated();
UFUNCTION()
void HandleWeaponReloadStarted();
public:
UFUNCTION(BlueprintImplementableEvent, Category = "Weapon|Reload")
void OnWeaponReloadStarted(AWeaponActor* ReloadingWeapon, EGunType ReloadWeaponType, EAmmoFeedType ReloadAmmoFeedType);
UFUNCTION(BlueprintCallable)
void SetSightTransform();
UFUNCTION(BlueprintCallable)
void SetRelativeHandTransform();
UFUNCTION(BlueprintCallable)
void SetFinalHandTransform();
UFUNCTION(BlueprintCallable)
void Reload();
UFUNCTION(BlueprintCallable)
void ReloadFinished();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment