Download

Spawn Component instead of ProjectileClass

Hi,

How to spawn component like BoxComponent or other component with my code. The main reason is because I need if component hit “Head” with boxcomponent then display text (check above). This code works but display projectileclass.

my code:

AActor* MyActor = GetRootComponent()->GetOwner();
if (MyActor && ProjectileClass)
{

    FVector EyeLocation;
    FRotator EyeRotation;
    MyActor->GetActorEyesViewPoint(EyeLocation, EyeRotation);

    FVector ShotDirection = EyeRotation.Vector();
    FVector TraceEnd = EyeLocation + (ShotDirection * 10000);

    FCollisionQueryParams QueryParams;
    QueryParams.AddIgnoredActor(MyActor);
    QueryParams.AddIgnoredActor(this);
    QueryParams.bTraceComplex = true;
    QueryParams.bReturnPhysicalMaterial = true;

    FVector MuzzleLocation = GetMesh()->GetSocketLocation("headSocket");


    FHitResult Hit;
    if (GetWorld()->LineTraceSingleByChannel(Hit, EyeLocation, TraceEnd, ECC_Visibility, QueryParams))
    {
        //UE_LOG(LogTemp, Warning, TEXT("Your message"));

        AActor* HitActor = Hit.GetActor();

        UGameplayStatics::ApplyPointDamage(HitActor, 20.0f, ShotDirection, Hit, MyActor->GetInstigatorController(), this, DamageType);
    }



    FActorSpawnParameters ActorSpawnParams;
    ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
    GetWorld()->SpawnActor<AActor>(ProjectileClass, MuzzleLocation, EyeRotation, ActorSpawnParams);




    EPhysicalSurface SurfaceType = UPhysicalMaterial::DetermineSurfaceType(Hit.PhysMaterial.Get());



    if (SurfaceType == Head)
    {
        UE_LOG(LogTemp, Warning, TEXT("Test"));
    }

You might be looking for:

In constructor:


UBoxComponent* boxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxComponent"));

or most other places


UBoxComponent* boxComponent = NewObject<UBoxComponent>(this, UBoxComponent::StaticClass());
boxComponent->RegisterComponent();