언리얼4 발사체 낙차 구현

Intro

  • 발사체 낙차 구현하기

발사체 액터 생성

생성자

RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
RootComponent = RootComp;

Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("SphereCollision"));
Sphere->SetupAttachment(RootComponent);

BulletMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BulletMesh"));
BulletMesh->SetupAttachment(RootComponent);

BulletParticles = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("ProjectileParticles"));
BulletParticles->SetupAttachment(RootComponent);

static ConstructorHelpers::FObjectFinder<UParticleSystem> Blood(TEXT("ParticleSystem'/Game/WeaponEffects/P_body_bullet_impact.P_body_bullet_impact'"));
if (Blood.Succeeded())
{
    BloodParticle = Blood.Object;
}

static ConstructorHelpers::FObjectFinder<UParticleSystem> Smoke(TEXT("PParticleSystem'/Game/WeaponEffects/P_AssaultRifle_IH.P_AssaultRifle_IH'"));
if (Smoke.Succeeded())
{
	SmokeParticle = Smoke.Object;
}

InitialLifeSpan = 5.0f;

날아가게할 액터의 메쉬 컴포넌트, 파티클 컴포넌트, 부딪혔을때 발생하는 파티클등을 추가한다.

InitialLifeSpan을 5.0으로두어 5초뒤 액터가 사라지도록한다.

낙차 구현

void ABullet::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	
	

	FVector StartTrace = GetActorLocation();
	FVector EndTrace = StartTrace + (Velocity*DeltaTime);

	FVector Gravity = UKismetMathLibrary::MakeVector(0, 0, -980.0);
	FVector CurrentGravity = Gravity * DeltaTime;

	FCollisionQueryParams CollisionParams;
	CollisionParams.AddIgnoredActor(this);

	FHitResult HitResult;
	if (GetWorld()->LineTraceSingleByChannel(HitResult, StartTrace, EndTrace, ECC_GameTraceChannel2, CollisionParams))
	{
		AActor* HitActor=HitResult.GetActor();
		if (HitResult.GetActor())
		{
			//DrawDebugSolidBox(GetWorld(), HitResult.ImpactPoint, FVector(10.0f), FColor::Blue, true);

			if (HitResult.BoneName != NAME_None)
			{
				UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), BloodParticle, HitResult.ImpactPoint, FRotator::ZeroRotator, FVector(3.0f, 3.0f, 3.0f));
				LOG(Warning, TEXT("%s, %s"), *HitResult.BoneName.ToString(),NETMODE_WORLD);
				
				if(HitResult.BoneName == TEXT("head"))
				{
					UGameplayStatics::ApplyPointDamage(HitActor, 100.0f, HitActor->GetActorLocation(), HitResult, nullptr, this, nullptr);

				}
				else 
				{
					UGameplayStatics::ApplyPointDamage(HitActor, 90.0f, HitActor->GetActorLocation(), HitResult, nullptr, this, nullptr);
				}
			}
			else
			{
				UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), SmokeParticle, HitResult.ImpactPoint, FRotator::ZeroRotator, FVector(3.0f, 3.0f, 3.0f));
			}
			Destroy();
		}
			
	}
	//DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor(255, 0, 255), false, 10.0f);

	SetActorLocation(EndTrace);
	Velocity = Velocity + CurrentGravity;

}
FVector NewVelocity = WeaponCamera->GetForwardVector()*24000.0f; 

FireWeapon(Transform,NewVelocity);

발사할 캐릭터에서 스폰될 총알액터의 Velocity를 정해준다.

위에처럼 전달받으면 무기 카메라(총구) 전방으로 초당 24000(240m/s) 이동하는 총알액터가 스폰된다.

이때 계속 Gravity(0,0,-980.0) 벡터값을 더해줌으로써 총알이 낙하하게 된다.

또한 라인 트레이스를 통해 충돌한 액터가 본을 가지고있으면 데미지와 피 이펙트를 주도록한다.

실행 결과

gif

참고 자료

https://youtu.be/Z7t3eyZsq34

댓글남기기