127 lines
3.5 KiB
C++
127 lines
3.5 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "MyCharacter.h"
|
|
|
|
#include "EnhancedInputSubsystems.h"
|
|
#include "EnhancedInputComponent.h"
|
|
#include "GameFramework/CharacterMovementComponent.h"
|
|
|
|
#include "CameraPawn.h"
|
|
|
|
void AMyCharacter::Move(const FInputActionValue& Value)
|
|
{
|
|
auto f2d = Value.Get<FVector2D>();
|
|
auto f = GetActorForwardVector();
|
|
auto r = GetActorRightVector();
|
|
AddMovementInput(f * f2d.X);
|
|
AddMovementInput(r * f2d.Y);
|
|
}
|
|
|
|
void AMyCharacter::BeginRun(const FInputActionValue& Value)
|
|
{
|
|
bIsRunning = true;
|
|
GetCharacterMovement()->MaxWalkSpeed = 1200.0f;
|
|
}
|
|
void AMyCharacter::StopRun(const FInputActionValue& Value)
|
|
{
|
|
bIsRunning = false;
|
|
GetCharacterMovement()->MaxWalkSpeed = 600.0f;
|
|
}
|
|
|
|
void AMyCharacter::BeginCrouch(const FInputActionValue& Value)
|
|
{
|
|
bIsCrouching = true;
|
|
GetCharacterMovement()->bWantsToCrouch = true;
|
|
GetCharacterMovement()->Crouch();
|
|
UE_LOG(LogTemp, Warning, TEXT("Crouching"));
|
|
}
|
|
|
|
void AMyCharacter::StopCrouch(const FInputActionValue& Value)
|
|
{
|
|
bIsCrouching = false;
|
|
GetCharacterMovement()->bWantsToCrouch = false;
|
|
GetCharacterMovement()->UnCrouch();
|
|
UE_LOG(LogTemp, Warning, TEXT("Stop Crouch"));
|
|
}
|
|
|
|
// 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;
|
|
|
|
GetCharacterMovement()->GetNavAgentPropertiesRef().bCanCrouch = true;
|
|
}
|
|
|
|
// Called when the game starts or when spawned
|
|
void AMyCharacter::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
auto w = GetWorld();
|
|
ACameraPawn* temp = nullptr;
|
|
if (w)
|
|
{
|
|
if (CameraActorClass)
|
|
{
|
|
FTransform SpawnTransform(GetActorRotation(), GetActorLocation());
|
|
temp = w->SpawnActorDeferred<ACameraPawn>(CameraActorClass, SpawnTransform);
|
|
temp->FollowTarget = this;
|
|
temp->FinishSpawning(SpawnTransform);
|
|
}
|
|
else
|
|
{
|
|
FTransform SpawnTransform(GetActorRotation(), GetActorLocation());
|
|
temp = w->SpawnActorDeferred<ACameraPawn>(ACameraPawn::StaticClass(), SpawnTransform);
|
|
temp->FollowTarget = this;
|
|
temp->FinishSpawning(SpawnTransform);
|
|
}
|
|
}
|
|
if (temp)
|
|
{
|
|
CameraActor = temp;
|
|
}
|
|
}
|
|
|
|
// Called every frame
|
|
void AMyCharacter::Tick(float DeltaTime)
|
|
{
|
|
Super::Tick(DeltaTime);
|
|
|
|
}
|
|
|
|
// Called to bind functionality to input
|
|
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
|
|
{
|
|
Super::SetupPlayerInputComponent(PlayerInputComponent);
|
|
|
|
auto PlayerController = Cast<APlayerController>(GetController());
|
|
UEnhancedInputLocalPlayerSubsystem* Subsystem = NULL;
|
|
if (PlayerController)
|
|
{
|
|
Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer());
|
|
}
|
|
if (Subsystem && DefaultMapping)
|
|
{
|
|
Subsystem->AddMappingContext(DefaultMapping, 0);
|
|
}
|
|
|
|
auto EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent);
|
|
if (EnhancedInputComponent)
|
|
{
|
|
if (MoveAction) {
|
|
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyCharacter::Move);
|
|
}
|
|
if (RunAction) {
|
|
EnhancedInputComponent->BindAction(RunAction, ETriggerEvent::Started, this, &AMyCharacter::BeginRun);
|
|
EnhancedInputComponent->BindAction(RunAction, ETriggerEvent::Completed, this, &AMyCharacter::StopRun);
|
|
}
|
|
if (CrouchAction) {
|
|
EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Started, this, &AMyCharacter::BeginCrouch);
|
|
EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Completed, this, &AMyCharacter::StopCrouch);
|
|
}
|
|
}
|
|
}
|
|
|