Add Aim Down Sights functionality

This commit is contained in:
Louis Hobbs 2023-07-18 22:21:28 +01:00
parent 87cd5f4242
commit 3ed84127c2
5 changed files with 71 additions and 14 deletions

BIN
Content/Input/Actions/IA_AimDownSights.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Input/InputMappingContext.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Player/PlayerCharacter.uasset (Stored with Git LFS)

Binary file not shown.

View File

@ -29,21 +29,30 @@ APlayerCharacter::APlayerCharacter()
//bUseControllerRotationRoll = false; //bUseControllerRotationRoll = false;
// Setup the camera boom // Setup the camera boom
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom")); CameraSpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArmComponent"));
CameraBoom->SetupAttachment(RootComponent); CameraSpringArmComponent->SetupAttachment(RootComponent);
CameraBoom->bDoCollisionTest = true; CameraSpringArmComponent->bDoCollisionTest = true;
CameraBoom->bUsePawnControlRotation = true; CameraSpringArmComponent->bUsePawnControlRotation = true;
CameraBoom->TargetArmLength = 350.0f; CameraSpringArmComponent->TargetArmLength = 350.0f;
CameraBoom->bEnableCameraLag = true; CameraSpringArmComponent->bEnableCameraLag = true;
CameraBoom->CameraLagSpeed = 10.0f; CameraSpringArmComponent->CameraLagSpeed = 10.0f;
CameraBoom->SocketOffset = {0.0f, 75.0f, 110.0f}; CameraSpringArmComponent->SocketOffset = {0.0f, 75.0f, 110.0f};
// Setup the camera component // Setup the camera component
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent")); CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComponent->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); CameraComponent->SetupAttachment(CameraSpringArmComponent, USpringArmComponent::SocketName);
CameraComponent->bUsePawnControlRotation = false; CameraComponent->bUsePawnControlRotation = false;
CameraComponent->SetRelativeRotation({-5.0f, 0.0f, 0.0f}); CameraComponent->SetRelativeRotation({-5.0f, 0.0f, 0.0f});
// Setup the camera sights boom
CameraADSSpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraADSSpringArmComponent"));
CameraADSSpringArmComponent->SetupAttachment(RootComponent);
CameraADSSpringArmComponent->bDoCollisionTest = true;
CameraADSSpringArmComponent->bUsePawnControlRotation = true;
CameraADSSpringArmComponent->TargetArmLength = 100.0f;
CameraADSSpringArmComponent->bEnableCameraLag = false;
CameraADSSpringArmComponent->SocketOffset = {0.0f, 50.0f, 75.0f};
// Setup the character movement // Setup the character movement
UCharacterMovementComponent* CharacterMovementComponent = GetCharacterMovement(); UCharacterMovementComponent* CharacterMovementComponent = GetCharacterMovement();
CharacterMovementComponent->AirControl = 1.0f; CharacterMovementComponent->AirControl = 1.0f;
@ -150,6 +159,14 @@ void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCom
Input->BindAction(WeaponSwitchingAction, ETriggerEvent::Triggered, this, Input->BindAction(WeaponSwitchingAction, ETriggerEvent::Triggered, this,
&APlayerCharacter::WeaponSwitchingCallback); &APlayerCharacter::WeaponSwitchingCallback);
} }
if (AimDownSightsAction)
{
Input->BindAction(AimDownSightsAction, ETriggerEvent::Started, this,
&APlayerCharacter::BeginAimDownSightsCallback);
Input->BindAction(AimDownSightsAction, ETriggerEvent::Completed, this,
&APlayerCharacter::EndAimDownSightsCallback);
}
} }
} }
@ -361,6 +378,30 @@ void APlayerCharacter::WeaponSwitchingCallback(const FInputActionInstance& Insta
} }
} }
void APlayerCharacter::BeginAimDownSightsCallback(const FInputActionInstance& Instance)
{
FLatentActionInfo LatentActionInfo;
LatentActionInfo.CallbackTarget = this;
CameraComponent->AttachToComponent(CameraADSSpringArmComponent, FAttachmentTransformRules::KeepWorldTransform,
USpringArmComponent::SocketName);
UKismetSystemLibrary::MoveComponentTo(CameraComponent, FVector::ZeroVector, FRotator::ZeroRotator, true, true,
CameraBlendTime,
true, EMoveComponentAction::Type::Move, LatentActionInfo);
}
void APlayerCharacter::EndAimDownSightsCallback(const FInputActionInstance& Instance)
{
FLatentActionInfo LatentActionInfo;
LatentActionInfo.CallbackTarget = this;
CameraComponent->AttachToComponent(CameraSpringArmComponent, FAttachmentTransformRules::KeepWorldTransform,
USpringArmComponent::SocketName);
UKismetSystemLibrary::MoveComponentTo(CameraComponent, FVector::ZeroVector, FRotator::ZeroRotator, true, true,
CameraBlendTime,
true, EMoveComponentAction::Type::Move, LatentActionInfo);
}
void APlayerCharacter::OnFire() void APlayerCharacter::OnFire()
{ {
if (!IsFiring) if (!IsFiring)

View File

@ -55,6 +55,9 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UInputAction* WeaponSwitchingAction; UInputAction* WeaponSwitchingAction;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UInputAction* AimDownSightsAction;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TSubclassOf<class UUserWidget> PlayerHUD; TSubclassOf<class UUserWidget> PlayerHUD;
@ -62,13 +65,19 @@ protected:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float SprintSpeedMultiplier = 2.0f; float SprintSpeedMultiplier = 2.0f;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float CameraBlendTime = 0.1f;
private: private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true")) UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true"))
USpringArmComponent* CameraBoom = nullptr; USpringArmComponent* CameraSpringArmComponent = nullptr;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true")) UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true"))
UCameraComponent* CameraComponent = nullptr; UCameraComponent* CameraComponent = nullptr;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true"))
USpringArmComponent* CameraADSSpringArmComponent = nullptr;
float DefaultMovementSpeed; float DefaultMovementSpeed;
FTimerHandle FireTimerHandle; FTimerHandle FireTimerHandle;
@ -117,6 +126,10 @@ public:
void WeaponSwitchingCallback(const FInputActionInstance& Instance); void WeaponSwitchingCallback(const FInputActionInstance& Instance);
void BeginAimDownSightsCallback(const FInputActionInstance& Instance);
void EndAimDownSightsCallback(const FInputActionInstance& Instance);
virtual void OnFire() override; virtual void OnFire() override;
void WeaponCooldownHandler(); void WeaponCooldownHandler();