From a04d9c436a44381e08c7d7fde6679fc9913299f6 Mon Sep 17 00:00:00 2001 From: JKuijperM Date: Tue, 27 Dec 2022 12:37:25 +0100 Subject: [PATCH] Work in the movement control of the player --- Content/Blueprints/BP_ZombieCharacter.uasset | 4 +- Source/EndlessZombie/ZombieCharacter.cpp | 73 +++++++++++++++++++- Source/EndlessZombie/ZombieCharacter.h | 7 ++ 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/Content/Blueprints/BP_ZombieCharacter.uasset b/Content/Blueprints/BP_ZombieCharacter.uasset index 2c4315e..ddb6f18 100644 --- a/Content/Blueprints/BP_ZombieCharacter.uasset +++ b/Content/Blueprints/BP_ZombieCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ff17ff3b535b746f266fb3b6de7e7c852de96005c84bd8eae82b2f65df17d01b -size 22998 +oid sha256:01a2ccb4fae05146826f110d944ac346e311548b5e59e5e8fe7477b6752d0862 +size 33555 diff --git a/Source/EndlessZombie/ZombieCharacter.cpp b/Source/EndlessZombie/ZombieCharacter.cpp index 4cf9283..4af971d 100644 --- a/Source/EndlessZombie/ZombieCharacter.cpp +++ b/Source/EndlessZombie/ZombieCharacter.cpp @@ -3,7 +3,13 @@ #include "ZombieCharacter.h" #include "Camera/CameraComponent.h" +#include "Components/CapsuleComponent.h" +#include "Components/InputComponent.h" +#include "GameFramework/CharacterMovementComponent.h" +#include "GameFramework/Controller.h" #include "GameFramework/SpringArmComponent.h" +#include "EnhancedInputComponent.h" +#include "EnhancedInputSubsystems.h" // Sets default values AZombieCharacter::AZombieCharacter() @@ -11,6 +17,26 @@ AZombieCharacter::AZombieCharacter() // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; + // Set size for collision capsule + GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f); + + // Don't rotate when the controller rotates. Let that just affect the camera. + bUseControllerRotationPitch = false; + bUseControllerRotationYaw = false; + bUseControllerRotationRoll = false; + + // Configure character movement + GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input... + GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...at this rotation rate + + // Note: For faster iteration times these variables, and many more, can be tweaked in the Character Blueprint + // instead of recompiling to adjust them + GetCharacterMovement()->JumpZVelocity = 700.f; + GetCharacterMovement()->AirControl = 0.35f; + GetCharacterMovement()->MaxWalkSpeed = 500.f; + GetCharacterMovement()->MinAnalogWalkSpeed = 20.f; + GetCharacterMovement()->BrakingDecelerationWalking = 2000.f; + // Create a camera boom (pulls in towards the player if there is a collision) CameraBoom = CreateDefaultSubobject(TEXT("CameraBoom")); @@ -28,7 +54,14 @@ AZombieCharacter::AZombieCharacter() void AZombieCharacter::BeginPlay() { Super::BeginPlay(); - + //Add Input Mapping Context + if (APlayerController* PlayerController = Cast(Controller)) + { + if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem(PlayerController->GetLocalPlayer())) + { + Subsystem->AddMappingContext(DefaultMappingContext, 0); + } + } } // Called every frame @@ -42,6 +75,44 @@ void AZombieCharacter::Tick(float DeltaTime) void AZombieCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); + // Set up action bindings + if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked(PlayerInputComponent)) + { + // Jumping + EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump); + EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping); + // Moving + EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AZombieCharacter::Move); + + // Crouching + EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Triggered, this, &AZombieCharacter::Crouch); + } } +void AZombieCharacter::Move(const FInputActionValue& Value) +{ + // input is a Vector2D + FVector2D MovementVector = Value.Get(); + if (Controller != nullptr) + { + // find out which way is forward + const FRotator Rotation = Controller->GetControlRotation(); + const FRotator YawRotation(0, Rotation.Yaw, 0); + + // get forward vector + const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X); + + // get right vector + const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y); + + // add movement + AddMovementInput(ForwardDirection, MovementVector.Y); + AddMovementInput(RightDirection, MovementVector.X); + } +} + +void AZombieCharacter::Crouch(const FInputActionValue& Value) +{ + +} \ No newline at end of file diff --git a/Source/EndlessZombie/ZombieCharacter.h b/Source/EndlessZombie/ZombieCharacter.h index ca9dcb1..3031726 100644 --- a/Source/EndlessZombie/ZombieCharacter.h +++ b/Source/EndlessZombie/ZombieCharacter.h @@ -32,6 +32,10 @@ class ENDLESSZOMBIE_API AZombieCharacter : public ACharacter UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true")) class UInputAction* MoveAction; + /** Crouch Input Action */ + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true")) + class UInputAction* CrouchAction; + public: // Sets default values for this character's properties AZombieCharacter(); @@ -43,6 +47,9 @@ protected: // APawn interface virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; + void Move(const FInputActionValue& Value); + void Crouch(const FInputActionValue& Value); + public: // Called every frame virtual void Tick(float DeltaTime) override;