Section 8: Advanced Movement - Lecture 117

Hip Aim Offset
This commit is contained in:
charnet3d 2023-04-03 23:45:05 +00:00
parent acd849d4fc
commit 56fad37200
11 changed files with 123 additions and 41 deletions

View File

@ -1,8 +1,8 @@
[/Script/EngineSettings.GameMapsSettings]
GameDefaultMap=/Game/ModSciInteriors/Maps/Example_2_Dynamic.Example_2_Dynamic
EditorStartupMap=/Game/ModSciInteriors/Maps/Example_2_Dynamic.Example_2_Dynamic
GameDefaultMap=/Game/_Game/Maps/DefaultMap.DefaultMap
EditorStartupMap=/Game/_Game/Maps/DefaultMap.DefaultMap
GlobalDefaultGameMode=/Game/_Game/GameMode/ShooterGameModeBaseBP.ShooterGameModeBaseBP_C
[/Script/HardwareTargeting.HardwareTargetingSettings]

Binary file not shown.

Binary file not shown.

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ea499b26b1ff7085faf02c40cb4642ecb7c60c09db55a5f7de4d85aea8e4be60
size 1199

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7b8c1a9f82a3ae31625ce118399a72146e4249f1c1b9c8dbdfffc91861ff6682
size 1215

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:47ee89e3a2a3584ffc0f9b4b2915c59f0a653ce08699975527a7dddfc6cd1dde
size 1223

Binary file not shown.

BIN
Content/_Game/Maps/DefaultMap.umap (Stored with Git LFS)

Binary file not shown.

View File

@ -7,36 +7,50 @@
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/KismetMathLibrary.h"
UShooterAnimInstance::UShooterAnimInstance() :
Speed(0.f),
bIsInAir(false),
bIsAccelerating(false),
MovementOffsetYaw(0.f),
LastMovementOffsetYaw(0.f),
bAiming(false),
CharacterYaw(0.f),
CharacterYawLastFrame(0.f),
RootYawOffset(0.f)
{
}
void UShooterAnimInstance::UpdateAnimationProperties(float DeltaTime)
{
if (ShooterCharacter)
if (!ShooterCharacter)
ShooterCharacter = Cast<AShooterCharacter>(TryGetPawnOwner());
if (ShooterCharacter)
if (!ShooterCharacter) return;
// Get the speed of the character from velocity
FVector Velocity{ ShooterCharacter->GetVelocity() };
Velocity.Z = 0;
Speed = Velocity.Size();
// Is the character in the air
bIsInAir = ShooterCharacter->GetCharacterMovement()->IsFalling();
// Is the character accelerating
bIsAccelerating = ShooterCharacter->GetCharacterMovement()->GetCurrentAcceleration().Size() > 0;
FRotator AimRotation = ShooterCharacter->GetBaseAimRotation();
FRotator MovementRotation = UKismetMathLibrary::MakeRotFromX(ShooterCharacter->GetVelocity());
MovementOffsetYaw = UKismetMathLibrary::NormalizedDeltaRotator(MovementRotation, AimRotation).Yaw;
if (ShooterCharacter->GetVelocity().Size() > 0)
{
// Get the speed of the character from velocity
FVector Velocity{ ShooterCharacter->GetVelocity() };
Velocity.Z = 0;
Speed = Velocity.Size();
// Is the character in the air
bIsInAir = ShooterCharacter->GetCharacterMovement()->IsFalling();
// Is the character accelerating
bIsAccelerating = ShooterCharacter->GetCharacterMovement()->GetCurrentAcceleration().Size() > 0;
FRotator AimRotation = ShooterCharacter->GetBaseAimRotation();
FRotator MovementRotation = UKismetMathLibrary::MakeRotFromX(ShooterCharacter->GetVelocity());
MovementOffsetYaw = UKismetMathLibrary::NormalizedDeltaRotator(MovementRotation, AimRotation).Yaw;
if (ShooterCharacter->GetVelocity().Size() > 0)
{
LastMovementOffsetYaw = MovementOffsetYaw;
}
bAiming = ShooterCharacter->GetAiming();
LastMovementOffsetYaw = MovementOffsetYaw;
}
bAiming = ShooterCharacter->GetAiming();
TurnInPlace();
}
void UShooterAnimInstance::NativeInitializeAnimation()
@ -45,3 +59,54 @@ void UShooterAnimInstance::NativeInitializeAnimation()
ShooterCharacter = Cast<AShooterCharacter>(TryGetPawnOwner());
}
void UShooterAnimInstance::TurnInPlace()
{
if (!ShooterCharacter) return;
if (Speed > 0)
{
// Don't want to turn in place; Character is moving
RootYawOffset = 0.f;
CharacterYaw = ShooterCharacter->GetActorRotation().Yaw;
CharacterYawLastFrame = CharacterYaw;
RotationCurve = 0.f;
RotationCurveLastFrame = 0.f;
}
else
{
CharacterYawLastFrame = CharacterYaw;
CharacterYaw = ShooterCharacter->GetActorRotation().Yaw;
const float YawDelta { CharacterYaw - CharacterYawLastFrame };
// Root Yaw Offset, updated and clamped to [-180, 180]
RootYawOffset = UKismetMathLibrary::NormalizeAxis(RootYawOffset - YawDelta);
// 1.0 if turning, 0.0 if not
const float Turning { GetCurveValue(TEXT("Turning")) };
if (Turning)
{
RotationCurveLastFrame = RotationCurve;
RotationCurve = GetCurveValue(TEXT("Rotation"));
const float DeltaRotation { RotationCurve = RotationCurveLastFrame };
// If RootYawOffset > 0, -> Turning Left, If RootYawOffset < 0, -> Turning Right
RootYawOffset > 0 ? RootYawOffset -= DeltaRotation : RootYawOffset += DeltaRotation;
const float ABSRootYawOffset { FMath::Abs(RootYawOffset) };
if (ABSRootYawOffset)
{
const float YawExcess { ABSRootYawOffset - 90.f };
RootYawOffset > 0 ? RootYawOffset -= YawExcess : RootYawOffset += YawExcess;
}
}
/*if (GEngine)
{
GEngine->AddOnScreenDebugMessage(1, -1, FColor::Blue,
FString::Printf(TEXT("CharacterYaw: %f"), CharacterYaw));
GEngine->AddOnScreenDebugMessage(2, -1, FColor::Red,
FString::Printf(TEXT("RootYawOffset: %f"), RootYawOffset));
}*/
}
}

View File

@ -17,11 +17,16 @@ class SHOOTER_API UShooterAnimInstance : public UAnimInstance
public:
UShooterAnimInstance();
UFUNCTION(BlueprintCallable)
void UpdateAnimationProperties(float DeltaTime);
virtual void NativeInitializeAnimation() override;
protected:
/** Handle turning in place variables */
void TurnInPlace();
private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = true))
@ -49,4 +54,19 @@ private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = true))
bool bAiming;
/** Yaw of the character this frame */
float CharacterYaw;
/** Yaw of the character the previous frame */
float CharacterYawLastFrame;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Turn In Place", meta = (AllowPrivateAccess = true))
float RootYawOffset;
/** Rotation curve value this frame */
float RotationCurve;
/** Rotation curve value last frame */
float RotationCurveLastFrame;
};