응애맘마조

231128 강의 본문

공부/UE강의

231128 강의

TH.Wert 2023. 11. 28. 20:35

어제의 강의 내용에 이어서 점프와 달리기 기능과 충돌 처리를 추가했습니다.

#include "C_Character.h"
#include "Global.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/CharacterMovementComponent.h"

// Sets default values
AC_Character::AC_Character()
{
	isRotation = false;
 	// 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;

	//메시 컴포넌트 안에(기존에있던 컴포넌트) 스켈레탈메시 로드해서 붙이기
	USkeletalMesh* mesh;
	CHelpers::GetAsset< USkeletalMesh>(&mesh, "SkeletalMesh'/Game/Character/Mesh/SK_Mannequin.SK_Mannequin'");
	GetMesh()->SetSkeletalMesh(mesh);
	GetMesh()->SetRelativeLocation(FVector(0, 0, -90));
	GetMesh()->SetRelativeRotation(FRotator(0, -90, 0));

	CHelpers::CreateComponent< USpringArmComponent>(this, &SpringArm, "SpringArm",
		GetCapsuleComponent());

	CHelpers::CreateComponent< UCameraComponent>(this, &Camera, "Camera",
		SpringArm);

	SpringArm->SetRelativeLocation(FVector(-30, 0, 60));
	SpringArm->SetRelativeRotation(FRotator(-30, 0, 0));
	SpringArm->bUsePawnControlRotation = true;
    
	TSubclassOf<UAnimInstance> animInstance;
	CHelpers::GetClass< UAnimInstance>(&animInstance,
		"AnimBlueprint'/Game/Scene2/BP/ABP_C_Character.ABP_C_Character_C'");
	GetMesh()->SetAnimInstanceClass(animInstance);

	GetCharacterMovement()->MaxWalkSpeed = 300.0f;
	GetCharacterMovement()->JumpZVelocity = 1000.0f;
}

// Called when the game starts or when spawned
void AC_Character::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void AC_Character::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

// Called to bind functionality to input
void AC_Character::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAxis("MoveForward", this, &AC_Character::OnMoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &AC_Character::OnMoveRight);
	PlayerInputComponent->BindAction("CamRotationLock", EInputEvent::IE_Pressed, this, 
		&AC_Character::CamRotationUnLock);
	PlayerInputComponent->BindAction("CamRotationLock", EInputEvent::IE_Released, this,
		&AC_Character::CamRotationLock);

	PlayerInputComponent->BindAxis("RotationYaw", this, &AC_Character::OnRotationYaw);
	PlayerInputComponent->BindAxis("RotationPitch", this, &AC_Character::OnRotationPitch);

	PlayerInputComponent->BindAction("PressRun", EInputEvent::IE_Pressed, this,
		&AC_Character::OnRunning);
	PlayerInputComponent->BindAction("PressRun", EInputEvent::IE_Released, this,
		&AC_Character::OnWalking);

	PlayerInputComponent->BindAction("Jumping", EInputEvent::IE_Pressed, this,
		&AC_Character::OnJumpping);
}

void AC_Character::OnMoveForward(float Axis)
{
	FRotator rotator = FRotator(0, GetControlRotation().Yaw, 0);
	FVector dir = FQuat(rotator).GetForwardVector().GetSafeNormal2D();
	AddMovementInput(dir, Axis);
}

void AC_Character::OnMoveRight(float Axis)
{
	FRotator rotator = FRotator(0, GetControlRotation().Yaw, 0);
	FVector dir = FQuat(rotator).GetRightVector().GetSafeNormal2D();
	AddMovementInput(dir, Axis);
}

void AC_Character::OnRotationYaw(float Axis)
{
	if(isRotation)
	AddControllerYawInput(Axis);
}

void AC_Character::OnRotationPitch(float Axis)
{
	if (isRotation)
	AddControllerPitchInput(Axis);
}

void AC_Character::CamRotationLock()
{
	isRotation = false;
}

void AC_Character::CamRotationUnLock()
{
	isRotation = true;
}

void AC_Character::OnRunning()
{
	GetCharacterMovement()->MaxWalkSpeed = 1000.0f;
}

void AC_Character::OnWalking()
{
	GetCharacterMovement()->MaxWalkSpeed = 300.0f;
}

void AC_Character::OnJumpping()
{
	Jump();
}

 

실행 영상

읽어주셔서 감사합니다.

'공부 > UE강의' 카테고리의 다른 글

231129 강의  (0) 2023.11.30
231129 강의  (1) 2023.11.29
231127 강의  (0) 2023.11.27
231124 강의  (0) 2023.11.25
231117 강의  (0) 2023.11.17
Comments