From 3ed84127c2dbdb369b42d13c99ed289c0d26c726 Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Tue, 18 Jul 2023 22:21:28 +0100 Subject: [PATCH] Add Aim Down Sights functionality --- Content/Input/Actions/IA_AimDownSights.uasset | 3 + Content/Input/InputMappingContext.uasset | 4 +- Content/Player/PlayerCharacter.uasset | 4 +- Source/Nakatomi/PlayerCharacter.cpp | 59 ++++++++++++++++--- Source/Nakatomi/PlayerCharacter.h | 15 ++++- 5 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 Content/Input/Actions/IA_AimDownSights.uasset diff --git a/Content/Input/Actions/IA_AimDownSights.uasset b/Content/Input/Actions/IA_AimDownSights.uasset new file mode 100644 index 0000000..f7a1647 --- /dev/null +++ b/Content/Input/Actions/IA_AimDownSights.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6e7e06884f27292694d40b2ebb5977824df34e43dacbe6ef98613a6abd914d98 +size 1375 diff --git a/Content/Input/InputMappingContext.uasset b/Content/Input/InputMappingContext.uasset index 6f8adae..7c0bdff 100644 --- a/Content/Input/InputMappingContext.uasset +++ b/Content/Input/InputMappingContext.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a202b29c730ab629b30f7d68ef530e107901a18a9f345104f62b9ccbb1aaa2ac -size 13853 +oid sha256:44aeafcc8593edb9e19576b5d819779645dd7416b7f5bae84fe0c6ba6f207c10 +size 16965 diff --git a/Content/Player/PlayerCharacter.uasset b/Content/Player/PlayerCharacter.uasset index dc47bcf..48def4b 100644 --- a/Content/Player/PlayerCharacter.uasset +++ b/Content/Player/PlayerCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:bdd9e6050d43bfac2d9da526fa7a27f7c1b5477805c29b0c1c1e6e913b929cf4 -size 37323 +oid sha256:bd5700e0af1f758f5449cf56c2faf50a9093d77b93352a17732a3a18033fb2b6 +size 37813 diff --git a/Source/Nakatomi/PlayerCharacter.cpp b/Source/Nakatomi/PlayerCharacter.cpp index 5185cee..f94954f 100644 --- a/Source/Nakatomi/PlayerCharacter.cpp +++ b/Source/Nakatomi/PlayerCharacter.cpp @@ -29,21 +29,30 @@ APlayerCharacter::APlayerCharacter() //bUseControllerRotationRoll = false; // Setup the camera boom - CameraBoom = CreateDefaultSubobject(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(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(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(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) diff --git a/Source/Nakatomi/PlayerCharacter.h b/Source/Nakatomi/PlayerCharacter.h index 63b4bce..95421b9 100644 --- a/Source/Nakatomi/PlayerCharacter.h +++ b/Source/Nakatomi/PlayerCharacter.h @@ -55,6 +55,9 @@ public: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UInputAction* WeaponSwitchingAction; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UInputAction* AimDownSightsAction; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) TSubclassOf 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();