// Fill out your copyright notice in the Description page of Project Settings. #include "M4_PlayerController.h" #include "EnhancedInputComponent.h" #include "Camera/CameraComponent.h" AM4_PlayerController::AM4_PlayerController() { CameraComponent = CreateDefaultSubobject(TEXT("CameraComponent")); CameraComponent->ProjectionMode = ECameraProjectionMode::Orthographic; CameraComponent->OrthoWidth = 2048.0f; CameraComponent->SetAspectRatio(160.0f/192.0f); // attach the camera to the controller's root component CameraComponent->SetupAttachment(GetRootComponent()); } void AM4_PlayerController::BeginPlay() { Super::BeginPlay(); if (CameraComponent) { CameraComponent->SetRelativeLocation(FVector(0, 0, 1000)); CameraComponent->SetRelativeRotation(FRotator(-90.0f, 0.0f, 0.0f)); // Set the camera as the view target SetViewTarget(this); } if (!GetLocalPlayer()) { PRINT_SCREEN(TEXT("No Local Player found!"), FColor::Red); return; } if (!ULocalPlayer::GetSubsystem(GetLocalPlayer())) { PRINT_SCREEN(TEXT("No Enhanced Input Local Player Subsystem found!"), FColor::Red); return; } // load the default mapping ctx ULocalPlayer::GetSubsystem(GetLocalPlayer())->AddMappingContext(DefaultMappingContext.LoadSynchronous(), 0); if (!DefaultMappingContext.IsValid()) { PRINT_SCREEN(TEXT("No Default Mapping Context found!"), FColor::Red); return; } PRINT_SCREEN(TEXT("Default Mapping Context loaded"), FColor::Green); } void AM4_PlayerController::SetupInputComponent() { Super::SetupInputComponent(); if (!InputComponent) { PRINT_SCREEN(TEXT("No Input Component found!"), FColor::Red); return; } if (!InputComponent->IsA(UEnhancedInputComponent::StaticClass())) { PRINT_SCREEN(TEXT("Input Component is not an Enhanced Input Component!"), FColor::Red); return; } // load the Move Action UInputAction* MoveActionLoaded = MoveAction.LoadSynchronous(); if (MoveActionLoaded) { PRINT_SCREEN(TEXT("Move Action loaded"), FColor::Green); } else { PRINT_SCREEN(TEXT("No Move Action found!"), FColor::Red); return; } // Bind the Move Action UEnhancedInputComponent* EnhancedInputComponent = CastChecked(InputComponent); if (EnhancedInputComponent) { EnhancedInputComponent->BindAction(MoveActionLoaded, ETriggerEvent::Triggered, this, &AM4_PlayerController::Move); PRINT_SCREEN(TEXT("Move Action bound"), FColor::Green); } } void AM4_PlayerController::Move(const FInputActionValue& Value) { GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Green, FString::Printf(TEXT("Move Value: %s"), *Value.ToString())); FVector2D MovementVector = Value.Get(); if (APawn* ControlledPawn = GetPawn()) { FVector NewLocation = ControlledPawn->GetActorLocation() + FVector(MovementVector.X, MovementVector.Y, 0.0f) * 10.0f; ControlledPawn->SetActorLocation(NewLocation); } } void AM4_PlayerController::OnPossess(APawn* InPawn) { Super::OnPossess(InPawn); PRINT_SCREEN(TEXT("PlayerController Possess"), FColor::Green); if (InPawn && CameraComponent) { CameraComponent->AttachToComponent(InPawn->GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform); CameraComponent->SetRelativeLocation(FVector(0, 0, 1000)); CameraComponent->SetRelativeRotation(FRotator(-90.0f, 0.0f, 0.0f)); } }