프로그래밍 공부
작성일
2024. 4. 23. 15:46
작성자
WDmil
728x90

기초 캐릭터를 응용해서 ABP를 연결할 수 있다.

 

플레이어블 케릭터의 ABP에 데이터를 연산, 전달할 수 있다.


Base_Character를 상속받는 Hero의 input을 정의한다.

// Fill out your copyright notice in the Description page of Project Settings.


#include "Actors/Heros/Hero.h"
#include "Utility/Helper.h"

#include "Engine/LocalPlayer.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"

#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/Controller.h"

#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "InputActionValue.h"

AHero::AHero()
{

	{
		SpringArm = Helper::CreateSceneComponent<USpringArmComponent>(this, "Spring Arm", GetCapsuleComponent());
		Camera = Helper::CreateSceneComponent<UCameraComponent>(this, "Camera", SpringArm);
	}

	//CameraSetting

	{
		SpringArm->SetRelativeLocation(FVector(0, 30, 60));
		SpringArm->bUsePawnControlRotation = false;

		GetCharacterMovement()->bUseControllerDesiredRotation = true;
		GetCharacterMovement()->bOrientRotationToMovement = true;
		GetCharacterMovement()->RotationRate = FRotator(0, 360, 0);
	}

}

void AHero::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	// Set up action bindings
	if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent)) {

		// Jumping
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &ACharacter::Jump);
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);

		// Moving
		EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AHero::Move);

		// Looking
		EnhancedInputComponent->BindAction(ViewAction, ETriggerEvent::Triggered, this, &AHero::Look);
	}
}

void AHero::BeginPlay()
{
	// Call the base class  
	Super::BeginPlay();

	//Add Input Mapping Context
	if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
	{
		if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
		{
			Subsystem->AddMappingContext(InputMappingContext, 0);
		}
	}
}

void AHero::Look(const FInputActionValue& Value)
{
	// input is a Vector2D
	FVector2D LookAxisVector = Value.Get<FVector2D>();

	if (Controller != nullptr)
	{
		// add yaw and pitch input to controller
		AddControllerYawInput(LookAxisVector.X);
		AddControllerPitchInput(LookAxisVector.Y);
	}
}

void AHero::Move(const FInputActionValue& Value)
{
	// input is a Vector2D
	FVector2D MovementVector = Value.Get<FVector2D>();

	if (Controller != nullptr)
	{
		// find out which way is forward
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);

		// get forward vector
		const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);

		// get right vector 
		const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

		// add movement 
		AddMovementInput(ForwardDirection, MovementVector.Y);
		AddMovementInput(RightDirection, MovementVector.X);
	}
}

 

객체의 현재 이동설정과 이동정의, Input컨트롤에 대한 설정을 할 수 있다.

 


에니메이션을 생성하기 위해, DefaltAnimInstance를 생성한다.

DefaltAnimInstance

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Animation/AnimInstance.h"
#include "DefaltAnimInstance.generated.h"

/**
 * 
 */
class ABaseCharacter;

UCLASS()
class FTPSGAME_API UDefaltAnimInstance : public UAnimInstance
{
	GENERATED_BODY()

	UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "BlendSpace" , meta = (AllowPrivateAccess = "true"))
	bool bFalling;

	UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "BlendSpace" , meta = (AllowPrivateAccess = "true"))
	float Speed;

	UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "BlendSpace" , meta = (AllowPrivateAccess = "true"))
	float Direction;

	UPROPERTY()
	TObjectPtr<ABaseCharacter> Owner;

public:
	virtual void NativeUpdateAnimation(float DeltaSeconds) override;

private:
	void UpdateBlendSpaceValue();

};

 

AninInstance를 사용할때, 값을 이용하기 위해 UPROPERTY() 로 사용하는, Falling(떨어지는지) Speed(현재속도) Direction(방향) 을 정의해놓는다.

 

C++에서만 사용할 수 있도록, ABaseCharacter 포인터 변수를 생성한다.

// Fill out your copyright notice in the Description page of Project Settings.


#include "Anim/DefaltAnimInstance.h"
#include "Utility/Helper.h"
#include "Actors/BaseCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "KismetAnimationLibrary.h"

void UDefaltAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
	Super::NativeUpdateAnimation(DeltaSeconds);

	Owner = Cast<ABaseCharacter>(TryGetPawnOwner());

	if (Owner == nullptr) return;

	UpdateBlendSpaceValue();
}

void UDefaltAnimInstance::UpdateBlendSpaceValue()
{
	bFalling = Owner->GetCharacterMovement()->IsFalling();

	Speed = Owner->GetVelocity().Size2D();

	FRotator Rotation = FRotator(0, Owner->GetActorRotation().Yaw, 0);
	Direction = CalculateDirection(Owner->GetVelocity(), Rotation);
}

생성자에서, Owner변수에 현재 Pawn의 owner를 지정해주고( ABP를 소유한 객체 ) 

 

UpdateBlendSpaceValue를 사용하여, 객체의 데이터값을 틱마다 업데이트 해준다.


ABP생성

ABP를 C++를 기준으로 정의하고 사용하기 위해 애니메이션 블루프린트 를 생성한다.

 

Defalt AnimInstance를 사용해서, 기본 Flaot값 등을 정의하고 사용할 수 있게 한다.

 

기반 블루프린트에서 정의하고 값을 사용한다.


테스트

 

 

 

 

728x90