// Fill out your copyright notice in the Description page of Project Settings. #include "MyPlayerCharacter/MyPlayerController.h" #include "EnhancedInputComponent.h" #include "EnhancedInputSubsystems.h" #include "Components/SplineMeshComponent.h" AMyPlayerController::AMyPlayerController() { } void AMyPlayerController::BeginPlay() { Super::BeginPlay(); PlayerCharacter = Cast(GetCharacter()); if (AMyPlayerController* PC = Cast(this)) { if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem(PC->GetLocalPlayer())) { if(PlayerInputContext) { Subsystem->AddMappingContext(PlayerInputContext, 0); } } } } void AMyPlayerController::SetupInputComponent() { Super::SetupInputComponent(); if (AMyPlayerController* PC = Cast(this)) { if (UEnhancedInputComponent* Subsystem = Cast(InputComponent)) { if (MoveAction) { Subsystem->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyPlayerController::Move); } if (JumpAction) { Subsystem->BindAction(JumpAction, ETriggerEvent::Started, this, &AMyPlayerController::Jump); } if (LookAction) { Subsystem->BindAction(LookAction, ETriggerEvent::Triggered, this, &AMyPlayerController::Look); } } } } void AMyPlayerController::Move(const FInputActionValue& Value) { FVector2D MovementVector = Value.Get(); if (APawn* ControlledPawn = GetPawn()) { FRotator CameraRotation = GetControlRotation(); FRotator YawRotation(0.f, CameraRotation.Yaw, 0.f); FVector ForwardDiection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X); FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y); ControlledPawn->AddMovementInput(ForwardDiection, MovementVector.Y); ControlledPawn->AddMovementInput(RightDirection,MovementVector.X); } } void AMyPlayerController::Look(const FInputActionValue& Value) { FVector2D LookAxisVector = Value.Get(); if (APawn* ControlledPawn = GetPawn()) { AddYawInput(LookAxisVector.X); AddPitchInput(LookAxisVector.Y); } } void AMyPlayerController::Jump(const FInputActionValue& Value) { if(PlayerCharacter) { PlayerCharacter->Jump(); } }