91 lines
2.2 KiB
C++
91 lines
2.2 KiB
C++
// 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<AMyCharacter>(GetCharacter());
|
|
|
|
if (AMyPlayerController* PC = Cast<AMyPlayerController>(this))
|
|
{
|
|
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
|
|
{
|
|
if(PlayerInputContext)
|
|
{
|
|
Subsystem->AddMappingContext(PlayerInputContext, 0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void AMyPlayerController::SetupInputComponent()
|
|
{
|
|
Super::SetupInputComponent();
|
|
if (AMyPlayerController* PC = Cast<AMyPlayerController>(this))
|
|
{
|
|
if (UEnhancedInputComponent* Subsystem = Cast<UEnhancedInputComponent>(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<FVector2D>();
|
|
|
|
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<FVector2D>();
|
|
|
|
if (APawn* ControlledPawn = GetPawn())
|
|
{
|
|
AddYawInput(LookAxisVector.X);
|
|
AddPitchInput(LookAxisVector.Y);
|
|
}
|
|
}
|
|
|
|
void AMyPlayerController::Jump(const FInputActionValue& Value)
|
|
{
|
|
if(PlayerCharacter)
|
|
{
|
|
PlayerCharacter->Jump();
|
|
}
|
|
|
|
}
|
|
|