UE5--008--Move-Jump-Look-Walk(Enhanced Input System)
1. MyThirdPersonChar蓝图
1.1 MyThirdPersonChar.h
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "Camera/CameraComponent.h" #include "GameFramework/SpringArmComponent.h" #include "GameFramework/Character.h" #include "InputActionValue.h" #include "MyThirdPersonChar.generated.h" UCLASS() class C002MYBLANKPROJ_API AMyThirdPersonChar : public ACharacter { GENERATED_BODY() // Arm for Camera UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MyTPS_Cam, meta = (AllowPrivateAccess = "true")) class USpringArmComponent* CameraBoom; // Follow camera UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MyTPS_Cam, meta = (AllowPrivateAccess = "true")) class UCameraComponent* FollowCamera; public: UPROPERTY(EditAnywhere, Category = Input) class UInputMappingContext* IC_Character; UPROPERTY(EditAnywhere, Category = Input) class UInputAction* IA_Move; UPROPERTY(EditAnywhere, Category = Input) class UInputAction* IA_Jump; UPROPERTY(EditAnywhere, Category = Input) class UInputAction* IA_Look; UPROPERTY(EditAnywhere, Category = Input) class UInputAction* IA_Walk; public: // Sets default values for this character's properties AMyThirdPersonChar(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; void Move(const FInputActionValue& Value); void Look(const FInputActionValue& Value); void BeginWalking(); void StopWalking(); public: // Called every frame virtual void Tick(float DeltaTime) override; };
1.2 MyThirdPersonChar.cpp
// Fill out your copyright notice in the Description page of Project Settings. #include "MyThirdPersonChar.h" #include "Components/CapsuleComponent.h" #include "GameFramework/CharacterMovementComponent.h" #include "EnhancedInputComponent.h" #include "EnhancedInputSubsystems.h" #include "InputMappingContext.h" #include "InputAction.h" #include "Kismet/KismetMathLibrary.h" #include "GameFramework/Controller.h" // Sets default values AMyThirdPersonChar::AMyThirdPersonChar() { // 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; // Create a camera boom (pulls in towards the player if there is a collision) CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom")); CameraBoom->SetupAttachment(RootComponent); CameraBoom->TargetArmLength = 300.0f; CameraBoom->bUsePawnControlRotation = true; // Create a camera that will follow the character FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera")); FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); FollowCamera->bUsePawnControlRotation = false; } // Called when the game starts or when spawned void AMyThirdPersonChar::BeginPlay() { Super::BeginPlay(); } // Called every frame void AMyThirdPersonChar::Tick(float DeltaTime) { Super::Tick(DeltaTime); } // Called to bind functionality to input void AMyThirdPersonChar::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); // UEnhancedInputComponent* EnhancedPlayerInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent); if (EnhancedPlayerInputComponent != nullptr) { APlayerController* PlayerController = Cast<APlayerController>(GetController()); UEnhancedInputLocalPlayerSubsystem* EnhancedSubsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()); EnhancedSubsystem->AddMappingContext(IC_Character, 1); } // if (EnhancedPlayerInputComponent != nullptr) { APlayerController* PlayerController = Cast<APlayerController>(GetController()); if (PlayerController != nullptr) { EnhancedPlayerInputComponent->BindAction(IA_Move, ETriggerEvent::Triggered, this, &AMyThirdPersonChar::Move); EnhancedPlayerInputComponent->BindAction(IA_Jump, ETriggerEvent::Started, this, &ACharacter::Jump); EnhancedPlayerInputComponent->BindAction(IA_Jump, ETriggerEvent::Completed, this, &ACharacter::StopJumping); EnhancedPlayerInputComponent->BindAction(IA_Look, ETriggerEvent::Triggered, this, &AMyThirdPersonChar::Look); EnhancedPlayerInputComponent->BindAction(IA_Walk, ETriggerEvent::Started, this, &AMyThirdPersonChar::BeginWalking); EnhancedPlayerInputComponent->BindAction(IA_Walk, ETriggerEvent::Completed, this, &AMyThirdPersonChar::StopWalking); } } } void AMyThirdPersonChar::Move(const FInputActionValue& Value) { FVector2D InputValue = Value.Get<FVector2D>(); if (Controller != nullptr && (InputValue.X != 0.0f || InputValue.Y != 0.0f)) { const FRotator YawRotation(0, Controller->GetControlRotation().Yaw, 0); if (InputValue.X != 0.0f) { const FVector RightDirection = UKismetMathLibrary::GetRightVector(YawRotation); AddMovementInput(RightDirection, InputValue.X); } if (InputValue.Y != 0.0f) { const FVector ForwardDirection = YawRotation.Vector(); AddMovementInput(ForwardDirection, InputValue.Y); } } } void AMyThirdPersonChar::Look(const FInputActionValue& Value) { FVector2D InputValue = Value.Get<FVector2D>(); if (InputValue.X != 0.0f) { AddControllerYawInput(InputValue.X); } if (InputValue.Y != 0.0f) { AddControllerPitchInput(InputValue.Y); } } void AMyThirdPersonChar::BeginWalking() { GetCharacterMovement()->MaxWalkSpeed *= 0.4f; } void AMyThirdPersonChar::StopWalking() { GetCharacterMovement()->MaxWalkSpeed /= 0.4f; }
2. Input Action
2.1 IA_Movement
2.2 IA_Jump
2.3 IA_Look
2.4 IA_Walk
3. IC_Character
3.1 IA_Movement
3.2 IA_Jump
3.3 IA_Look
3.4 IA_Walk
4. BP_MyTPC蓝图(继承MyThirdPersonChar蓝图)
                    
                








                
            
        
浙公网安备 33010602011771号