1. [Unreal 5 / C++] 슈팅 구현 하기

2024. 12. 31. 20:59·Unreal 5/FPS Shooting

이번 개발의 목표는 아무 플레이어 캐릭터에 직접 만든 컴포넌트를 붙이는 것 만으로 슈팅 시스템이 생기는 것을 목표로 하였다.


1. WeaponComponent 생성

캐릭터에 붙일 컴포넌트를 만들기 위해 ActorComponent를 상속받는 WeaponComponent 클래스를 생성한다.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Player/WeaponData.h"
#include "Components/SkeletalMeshComponent.h"
#include "Player/Ui/UiComponent.h"
#include "WeaponComponent.generated.h"

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class PROJECT_4_API UWeaponComponent : public UActorComponent
{
    GENERATED_BODY()

public:    
    UWeaponComponent();

protected:    
    virtual void BeginPlay() override;

public:    
    virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon")
    UWeaponData *WeaponData;

    UFUNCTION(BlueprintCallable, Category = "Fire") // For debug
    void FireWeapon();
};

2. 스켈레탈 메시 가져오기

BeginPlay에서 캐릭터의 스켈레탈 메시를 가져와 총구 위치를 참조한다.

void UWeaponComponent::BeginPlay()
{
    Super::BeginPlay();

    ACharacter* OwnerCharacter = Cast<ACharacter>(GetOwner());
    if (OwnerCharacter)
    {
        SkeletalMeshComponent = OwnerCharacter->GetMesh();
    }
}

3. 총 발사 로직

(1) 총구 위치 계산

스켈레탈 메시에서 SocketTransform을 가져와 월드 좌표계를 기준으로 총구 위치와 방향을 구한다.

본 프로젝트는 언리얼에서 제공하는 Paragon 에셋을 활용하였으며, 각자 본인이 원하는 스켈레탈 메시의 총구 위치에 해당하는 Socket 이름을 참조하면 된다.

FTransform SocketTransform = SkeletalMeshComponent->GetSocketTransform("Muzzle", RTS_World);
FVector MuzzleLocation = SocketTransform.GetLocation();
FRotator MuzzleRotation = SocketTransform.GetRotation().Rotator();

Paragon asset에서 총구 위치에 해당하는 소켓 이름

(2) 화면 중심 방향 계산

화면 중심에서 월드 방향으로 Ray를 쏘고, 충돌 여부에 따라 발사체 방향을 설정한다.

// 화면 중심 좌표 계산
FVector2D ViewportSize;
GEngine->GameViewport->GetViewportSize(ViewportSize);
FVector2D ScreenCenter(ViewportSize.X / 2, ViewportSize.Y / 2);

// 화면 중심에서 월드 방향 가져오기
FVector WorldLocation, WorldDirection;
UGameplayStatics::DeprojectScreenToWorld(UGameplayStatics::GetPlayerController(GetWorld(), 0), ScreenCenter, WorldLocation, WorldDirection);

// Ray 설정
FVector Start = WorldLocation;
FVector End = Start + (WorldDirection * 10000); // Ray 길이
FHitResult HitResult;

FVector HitLocation;
if (GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECC_Visibility))
{
    HitLocation = HitResult.Location; // 충돌한 위치
}
else
{
    HitLocation = End; // 충돌하지 않으면 Ray 끝점
}

// 발사 방향 계산
FVector ShootDirection = (HitLocation - MuzzleLocation).GetSafeNormal();

(3) 산탄 효과 추가

AimSize 값을 활용하여 총알이 랜덤하게 퍼지는 효과를 추가한다.

float AimSize = UiComponent->GetAimSize();
float RandomConeHalfAngle = FMath::DegreesToRadians(AimSize / 100.0f);
ShootDirection = FMath::VRandCone(ShootDirection, RandomConeHalfAngle);

(4) 발사체 생성 및 발사

ShootDirection에 따라 발사체를 생성하고, 설정된 속도로 발사한다.

FActorSpawnParameters SpawnParams;
AProjectile* Projectile = GetWorld()->SpawnActor<AProjectile>(AProjectile::StaticClass(), MuzzleLocation, ShootDirection.Rotation(), SpawnParams);
if (Projectile)
{
    Projectile->ShootInDirection(ShootDirection, WeaponData->ProjectileSpeed);
}

 

 

다음 글에서는 발사체, 즉 Projectile 클래스의 구현에 대해 알아보도록 하자

'Unreal 5 > FPS Shooting' 카테고리의 다른 글

5. [Unreal 5 / C++] 발사체와 오브젝트 풀링  (1) 2025.01.08
4. [Unreal 5 / C++] 반동(Camera Shake)과 연사  (2) 2025.01.07
3. [Unreal 5 / C++] 동적 크로스헤어 구현  (1) 2025.01.03
2. [Unreal 5 / C++] 발사체 구현 하기  (2) 2025.01.02
'Unreal 5/FPS Shooting' 카테고리의 다른 글
  • 5. [Unreal 5 / C++] 발사체와 오브젝트 풀링
  • 4. [Unreal 5 / C++] 반동(Camera Shake)과 연사
  • 3. [Unreal 5 / C++] 동적 크로스헤어 구현
  • 2. [Unreal 5 / C++] 발사체 구현 하기
돼지표
돼지표
https://github.com/wkdgns135
  • 돼지표
    돼지표 개발 스토리
    돼지표
  • 전체
    오늘
    어제
    • 분류 전체보기 (105) N
      • C++ (60)
        • Algorithm (53)
        • Study (1)
      • Python (1)
      • Machine Learning (2)
      • Computer Graphics (4)
        • Curly Hair Simulation (2)
      • GPU Programming (11)
        • CUDA basic (7)
        • CUDA fluidsGL (4)
      • Unreal 5 (21)
        • Troubleshooting (4)
        • FPS Shooting (5)
        • Study (10)
        • EOS (1)
      • Computer Science (6) N
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • Github
  • 공지사항

  • 인기 글

  • 태그

    BFS
    FPS
    Rendering Pipeline
    GPU Programming
    아이작 맵 생성
    UE5
    CUDA
    C++
    구현
    dp
    수학
    CS
    Fluid Simulation
    Algorithm
    OpenGL
    그래프 탐색
    정렬
    자료 구조
    위상 정렬
    unreal 5
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
돼지표
1. [Unreal 5 / C++] 슈팅 구현 하기
상단으로

티스토리툴바