Skip to content

Instantly share code, notes, and snippets.

@liana-p
Created January 28, 2025 20:33
Show Gist options
  • Select an option

  • Save liana-p/5faca070ca12deba1b99450e25a96f20 to your computer and use it in GitHub Desktop.

Select an option

Save liana-p/5faca070ca12deba1b99450e25a96f20 to your computer and use it in GitHub Desktop.
EnhancedInput version of BlasterCharacter cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterCharacter.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
// Sets default values
ABlasterCharacter::ABlasterCharacter()
{
// 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;
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(GetMesh());
CameraBoom->TargetArmLength = 600.0f;
CameraBoom->bUsePawnControlRotation = true;
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false;
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
}
void ABlasterCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* Input = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
if (MoveAction)
{
UE_LOG(LogTemp, Display, TEXT("Binding MoveAction"));
Input->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ABlasterCharacter::Move);
}
else
{
UE_LOG(LogTemp, Error, TEXT("MoveAction is null!"));
}
Input->BindAction(LookAction, ETriggerEvent::Triggered, this, &ABlasterCharacter::Look);
Input->BindAction(JumpAction, ETriggerEvent::Started, this, &ABlasterCharacter::Jump);
}
}
// Called when the game starts or when spawned
void ABlasterCharacter::BeginPlay()
{
Super::BeginPlay();
if (Controller)
{
UE_LOG(LogTemp, Display, TEXT("%s is possessed by %s"), *GetName(), *Controller->GetName());
}
else
{
UE_LOG(LogTemp, Error, TEXT("%s is not possessed"), *GetName());
}
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
UE_LOG(LogTemp, Display, TEXT("Player Controller is %s"), *PlayerController->GetName());
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
if (InputMapping)
{
Subsystem->AddMappingContext(InputMapping, 0);
UE_LOG(LogTemp, Display, TEXT("Input Mapping Context added"));
}
else
{
UE_LOG(LogTemp, Error, TEXT("InputMapping is null!"));
}
}
}
}
void ABlasterCharacter::Move(const FInputActionInstance& Instance)
{
FVector2D MovementDirection = Instance.GetValue().Get<FVector2D>();
const FRotator Rotation(0.f, Controller->GetControlRotation().Yaw, Controller->GetControlRotation().Roll);
const FVector RightDirection( FRotationMatrix(Rotation).GetUnitAxis(EAxis::Y));
const FVector ForwardDirection( FRotationMatrix(Rotation).GetUnitAxis(EAxis::X));
AddMovementInput(RightDirection, MovementDirection.X);
AddMovementInput(ForwardDirection, MovementDirection.Y);
}
void ABlasterCharacter::Look(const FInputActionInstance& Instance)
{
FVector2D LookDirection = Instance.GetValue().Get<FVector2D>();
AddControllerYawInput(LookDirection.X);
AddControllerPitchInput(LookDirection.Y);
}
void ABlasterCharacter::Jump(const FInputActionInstance& Instance)
{
Super::Jump();
UE_LOG(LogTemp, Display, TEXT("JumpAction"));
}
// Called every frame
void ABlasterCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment