51 - Leaning and Strafing

This commit is contained in:
Kingsmedia 2022-05-02 15:40:33 +02:00
parent 2125d3ee9f
commit 0729f5145e
7 changed files with 41 additions and 1 deletions

View File

@ -5,6 +5,7 @@
#include "BlasterCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/KismetMathLibrary.h"
void UBlasterAnimInstance::NativeInitializeAnimation()
{
@ -33,4 +34,18 @@ void UBlasterAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
bWeaponEquipped = BlasterCharacter->IsWeaponEquipped();
bIsCrouched = BlasterCharacter->bIsCrouched;
bAiming = BlasterCharacter->IsAiming();
// 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);
}

View File

@ -41,4 +41,14 @@ private:
UPROPERTY(BlueprintReadOnly, Category="Movement", meta=(AllowPrivateAccess = "true"))
bool bAiming;
UPROPERTY(BlueprintReadOnly, Category="Movement", meta=(AllowPrivateAccess = "true"))
float YawOffset;
UPROPERTY(BlueprintReadOnly, Category="Movement", meta=(AllowPrivateAccess = "true"))
float Lean;
FRotator CharacterRotationLastFrame;
FRotator CharacterRotation;
FRotator DeltaRotation;
};

View File

@ -6,6 +6,7 @@
#include "Blaster/Character/BlasterCharacter.h"
#include "Blaster/Weapon/Weapon.h"
#include "Engine/SkeletalMeshSocket.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Net/UnrealNetwork.h"
UCombatComponent::UCombatComponent()
@ -24,6 +25,15 @@ void UCombatComponent::SetAiming(bool bIsAiming)
ServerSetAiming(bIsAiming);
}
void UCombatComponent::OnRep_EquippedWeapon()
{
if (EquippedWeapon && Character)
{
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
Character->bUseControllerRotationYaw = true;
}
}
void UCombatComponent::ServerSetAiming_Implementation(bool bIsAiming)
{
bAiming = bIsAiming;
@ -55,4 +65,6 @@ void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip)
HandSocket->AttachActor(EquippedWeapon, Character->GetMesh());
}
EquippedWeapon->SetOwner(Character);
Character->GetCharacterMovement()->bOrientRotationToMovement = false;
Character->bUseControllerRotationYaw = true;
}

View File

@ -28,10 +28,13 @@ protected:
UFUNCTION(Server, Reliable)
void ServerSetAiming(bool bIsAiming);
UFUNCTION()
void OnRep_EquippedWeapon();
private:
class ABlasterCharacter* Character;
UPROPERTY(Replicated)
UPROPERTY(ReplicatedUsing=OnRep_EquippedWeapon)
class AWeapon* EquippedWeapon;
UPROPERTY(Replicated)