blaster/Source/Blaster/Character/BlasterAnimInstance.cpp

78 lines
3.2 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterAnimInstance.h"
#include "BlasterCharacter.h"
#include "Blaster/Weapon/Weapon.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/KismetMathLibrary.h"
void UBlasterAnimInstance::NativeInitializeAnimation()
{
Super::NativeInitializeAnimation();
BlasterCharacter = Cast<ABlasterCharacter>(TryGetPawnOwner());
}
void UBlasterAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
Super::NativeUpdateAnimation(DeltaSeconds);
if (BlasterCharacter == nullptr)
{
BlasterCharacter = Cast<ABlasterCharacter>(TryGetPawnOwner());
}
if (BlasterCharacter == nullptr) return;
FVector Velocity = BlasterCharacter->GetVelocity();
Velocity.Z = 0.f;
Speed = Velocity.Size();
bIsInAir = BlasterCharacter->GetCharacterMovement()->IsFalling();
bIsAccelerating = BlasterCharacter->GetCharacterMovement()->GetCurrentAcceleration().Size() > 0.f;
bWeaponEquipped = BlasterCharacter->IsWeaponEquipped();
EquippedWeapon = BlasterCharacter->GetEquippedWeapon();
bIsCrouched = BlasterCharacter->bIsCrouched;
bAiming = BlasterCharacter->IsAiming();
TurningInPlace = BlasterCharacter->GetTurningInPlace();
bRotateRootBone = BlasterCharacter->ShouldRotateRootBone();
bEliminated = BlasterCharacter->IsEliminated();
// Offset Yaw for Strafing
FRotator AimRotation = BlasterCharacter->GetBaseAimRotation();
FRotator MovementRotation = UKismetMathLibrary::MakeRotFromX(BlasterCharacter->GetVelocity());
FRotator DeltaRot = UKismetMathLibrary::NormalizedDeltaRotator(MovementRotation, AimRotation);
DeltaRotation = FMath::RInterpTo(DeltaRotation, DeltaRot, DeltaSeconds, 6.f);
YawOffset = DeltaRotation.Yaw;
CharacterRotationLastFrame = CharacterRotation;
CharacterRotation = BlasterCharacter->GetActorRotation();
const FRotator Delta = UKismetMathLibrary::NormalizedDeltaRotator(CharacterRotation, CharacterRotationLastFrame);
const float Target = Delta.Yaw / DeltaSeconds;
const float Interp = FMath::FInterpTo(Lean, Target, DeltaSeconds, 6.f);
Lean = FMath::Clamp(Interp, -90.f, 90.f);
// Aim offset
AO_Yaw = BlasterCharacter->GetAO_Yaw();
AO_Pitch = BlasterCharacter->GetAO_Pitch();
if (bWeaponEquipped && EquippedWeapon && EquippedWeapon->GetWeaponMesh() && BlasterCharacter->GetMesh())
{
LeftHandTransform = EquippedWeapon->GetWeaponMesh()->GetSocketTransform(FName("LeftHandSocket"), RTS_World);
FVector OutPosition;
FRotator OutRotation;
BlasterCharacter->GetMesh()->TransformToBoneSpace(FName("hand_r"), LeftHandTransform.GetLocation(), FRotator::ZeroRotator, OutPosition, OutRotation);
LeftHandTransform.SetLocation(OutPosition);
LeftHandTransform.SetRotation(FQuat(OutRotation));
if (BlasterCharacter->IsLocallyControlled())
{
bLocallyControlled = true;
const FTransform RightHandTransform = EquippedWeapon->GetWeaponMesh()->GetSocketTransform(FName("Hand_R"), RTS_World);
FRotator LookAtRotation = UKismetMathLibrary::FindLookAtRotation(RightHandTransform.GetLocation(), RightHandTransform.GetLocation() + (RightHandTransform.GetLocation() - BlasterCharacter->GetHitTarget()));
RightHandRotation = FMath::RInterpTo(RightHandRotation, LookAtRotation, DeltaSeconds, 30.f);
}
}
}