[UE5 FPS 프로젝트] 1. 1인칭 구현 및 무기 부착

2024. 6. 12. 19:53·언리얼엔진5/FPS 게임 1인 프로젝트

 

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

	UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent);

	EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
	EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
	EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AVICharacter::Move);
	EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &AVICharacter::Look);

}
void AVICharacter::SetupCharacterControl()
{
	if (!IsLocallyControlled()) return;
	
		APlayerController* PlayerController = CastChecked<APlayerController>(GetController());

		if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
		{
			if (Subsystem)
			{
				Subsystem->AddMappingContext(DefaultMappingContext, 0);
				//Subsystem->RemoveMappingContext(DefaultMappingContext);

			}
		}
	
}

 

 

위 코드들은 기본적인 조작을 담당하는 코드의 일부입니다.

 

    1인칭 캐릭터는 템플릿 기반으로 제작해 크게 설명할 것이 없을것으로 보입니다. 

 

템플릿을 사용해 1인칭 캐릭터를 생성하고 constructorhelpers를 통해 에셋들을 지정해 주었습니다.

 

협업의 편의성을 위해 함수의 실행은 블루프린트에서 하되, 구현은 C++에서 하였습니다. 디자이너들이 로직을 이해할 필요가 있을 때 유용할 수 있을 것 같습니다.

 

BlueprintCallable 로 선언하여 실행한 모습

 

 

SpawnInitialPrimaryWeapon.cpp

void AVICharacter::SpawnInitialPrimaryWeapon()
{
	if (UWorld* World = GetWorld())
	{
		if (PrimaryWeaponBpRef != nullptr)
		{
			// 스폰할 위치와 회전을 지정합니다.
			FVector SpawnLocation = FVector(0.0f, 0.0f, 0.0f);
			FRotator SpawnRotation = FRotator(0.0f, 0.0f, 0.0f);

			// 액터 스폰 파라미터 설정
			FActorSpawnParameters SpawnParams;
			SpawnParams.Owner = this;
			SpawnParams.Instigator = GetInstigator();

			// 월드에서 액터를 스폰합니다.
			PrimaryWeapon = World->SpawnActor<AActor>(PrimaryWeaponBpRef, SpawnLocation, SpawnRotation, SpawnParams);

			if (PrimaryWeapon)
			{
				// 스폰된 액터에 대해 추가 작업을 수행할 수 있습니다.
				FAttachmentTransformRules AttachmentRules(EAttachmentRule::KeepRelative, true); 

				PrimaryWeapon->AttachToComponent(FirstPersonMesh, AttachmentRules, FName(TEXT("Palm_R")));

			}
			else
			{
				UE_LOG(LogTemp, Log, TEXT("Failed to attach"));
			}
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("MyBlueprintActorClass is null"));
		}
	}
}

 

 

   위 코드를 이용해 액터를 월드 원점 좌표에 소환시킨 뒤 플레이어의 손바닥 소켓에 총을 연결해 주었습니다.

 

애니메이션 블루프린트도 간단하게 구현 해 주었는데, 아래와 같습니다.

 

매 틱마다 호출되는 이벤트.

 

 

++) 24.06.12

매 틱마다 Casting 을 수행하는 것은 성능에 악영향을 줄 수 있다고 판단되어 아래와 같이 Try Get Pawn Owner로 포인터를 간편히 가져오도록 수정 했습니다.

 

Try Get Pawn Owner 사용

 

 

 

 

 

AnimGraph

 

   애니메이션 그래프는 기본적인 Locomotion을 따르도록 설계 되어 있습니다.

 

아래는 작업을 수행한 뒤의 실행 화면입니다.

실행 화면

 

 

깃허브 

https://github.com/chataeg/VI

 

GitHub - chataeg/VI

Contribute to chataeg/VI development by creating an account on GitHub.

github.com

 

'언리얼엔진5 > FPS 게임 1인 프로젝트' 카테고리의 다른 글

[UE5 FPS 프로젝트] 6. ADS(조준) 시스템  (0) 2024.06.18
[UE5 FPS 프로젝트] 5. 총 추가 구현 및 Swap  (0) 2024.06.17
[UE5 FPS 프로젝트] 4. 총알 궤적, 총구 화염, 총알 자국, 탄퍼짐 구현  (0) 2024.06.14
[UE5 FPS 프로젝트] 3. HUD 및 피격 구현  (0) 2024.06.13
[UE5 FPS 프로젝트] 2. 발사 및 재장전 구현  (0) 2024.06.13
'언리얼엔진5/FPS 게임 1인 프로젝트' 카테고리의 다른 글
  • [UE5 FPS 프로젝트] 5. 총 추가 구현 및 Swap
  • [UE5 FPS 프로젝트] 4. 총알 궤적, 총구 화염, 총알 자국, 탄퍼짐 구현
  • [UE5 FPS 프로젝트] 3. HUD 및 피격 구현
  • [UE5 FPS 프로젝트] 2. 발사 및 재장전 구현
Rocketbabydolls
Rocketbabydolls
Rocketbabydolls
  • Rocketbabydolls
    With The Lights Out
    Rocketbabydolls
  • 전체
    오늘
    어제
    • 전체글 (184)
      • 프로그래밍 공부 (117)
        • C (16)
        • Jumping into C++ (9)
        • MFC (C++) (1)
        • 자료구조 (1)
        • 알고리즘 (1)
        • 백준 (C++) (74)
        • 핸즈온 머신러닝 2판 (6)
        • Unseen 3기 준비 (4)
        • 원티드 포텐업 게임 개발자 양성과정 2기 (4)
      • 언리얼엔진5 (63)
        • [Part1] 이득우의 언리얼 프로그래밍 (12)
        • [Part2] 이득우의 언리얼 프로그래밍 (2)
        • [Part2 복습] 이득우의 언리얼 프로그래밍 (3)
        • [Part3] 이득우의 언리얼 프로그래밍 (14)
        • [Part4] 이득우의 언리얼 프로그래밍 (0)
        • FPS 게임 1인 프로젝트 (6)
        • 각종 지식 (11)
        • 블루프린트 Paper2D 로 게임 만들기 (14)
        • 팀 프로젝트 (1)
      • 일상 (1)
      • ETC (1)
        • 맥북 (1)
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      실전 C프로그래밍
      언리얼엔진 eqs generator
      언리얼엔진5
      핸즈온 머신러닝 2판
      언리얼엔진 eqs 커스텀
      실전C프로그래밍 실습문제
      언리얼엔진
      언리얼엔진5 fps 프로젝트
      실전 C프로그래밍 나중채
      실전 C프로그래밍 실습문제
      c++ 17298
      c언어
      실전C프로그래밍 나중채
      실전C프로그래밍
      핸즈온 머신러닝
      티스토리챌린지
      실전 C 프로그래밍
      언리얼엔진 eqs c++
      C언어 실습문제
      오블완
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.3
    Rocketbabydolls
    [UE5 FPS 프로젝트] 1. 1인칭 구현 및 무기 부착
    상단으로

    티스토리툴바