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;
// Setup the camera boom
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->bDoCollisionTest = true;
CameraBoom->bUsePawnControlRotation = true;
CameraBoom->TargetArmLength = 350.0f;
CameraBoom->bEnableCameraLag = true;
CameraBoom->CameraLagSpeed = 10.0f;
CameraBoom->SocketOffset = {0.0f, 75.0f, 110.0f};
CameraSpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArmComponent"));
CameraSpringArmComponent->SetupAttachment(RootComponent);
CameraSpringArmComponent->bDoCollisionTest = true;
CameraSpringArmComponent->bUsePawnControlRotation = true;
CameraSpringArmComponent->TargetArmLength = 350.0f;
CameraSpringArmComponent->bEnableCameraLag = true;
CameraSpringArmComponent->CameraLagSpeed = 10.0f;
CameraSpringArmComponent->SocketOffset = {0.0f, 75.0f, 110.0f};
// Setup the camera component
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComponent->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
CameraComponent->SetupAttachment(CameraSpringArmComponent, USpringArmComponent::SocketName);
CameraComponent->bUsePawnControlRotation = false;
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
UCharacterMovementComponent* CharacterMovementComponent = GetCharacterMovement();
CharacterMovementComponent->AirControl = 1.0f;
@ -150,6 +159,14 @@ void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCom
Input->BindAction(WeaponSwitchingAction, ETriggerEvent::Triggered, this,
&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()
{
if (!IsFiring)

View File

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