Test Character and controller

This commit is contained in:
Kukukakhew
2025-11-22 13:06:39 -05:00
parent 0598ac722c
commit 1a9364c821
13 changed files with 226 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyGameModeBase.h"

View File

@@ -0,0 +1,23 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPlayerCharacter/MyCharacter.h"
// Sets default values
AMyCharacter::AMyCharacter()
{
// 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;
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComponent->SetupAttachment(RootComponent);
CameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f));
CameraComponent->bUsePawnControlRotation = true;
}
// Called when the game starts or when spawned
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
}

View File

@@ -0,0 +1,85 @@
// 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)
{
}