Crash on GetWorld()->LineTraceSingleByChannel

Hi.
I’m trying to make a gun fire using line trace.
I binded it to LMB, but whenever I press it, it crashes my engine.
Here’s my code:

FCollisionQueryParams FireTraceParams = FCollisionQueryParams(FName(TEXT("FireTrace")), true, this);
    	FireTraceParams.bTraceComplex = true;
    	FireTraceParams.bTraceAsyncScene = true;
    	FireTraceParams.bReturnPhysicalMaterial = false;
    	FHitResult FireHit(ForceInit);
    
    	UWorld* const World = GetWorld();
    	FVector StartLocation;
    	StartLocation = GetActorLocation();
    	FVector EndLocation;
    	EndLocation = GetActorForwardVector() + 100000000.0f;
    
    	if (World)
    	{
    		World->LineTraceSingleByChannel(FireHit, StartLocation, EndLocation, ECC_Pawn, FireTraceParams);
    
    		ProjectileParticleComponent = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("Projectile Hit Particle Component"));
    		ProjectileParticleComponent->SetWorldLocation(FireHit.ImpactPoint);
    		ProjectileParticleComponent->SetTemplate(ProjectileParticleSystem);
    		ProjectileParticleComponent->ActivateSystem();
    		delete ProjectileParticleComponent;
    	}

IMPORTANT EDIT:
I commented out the particle component part, and it works. Can you tell me, how to spawn a particle effect at the impact location of a line trace? I’d like to avoid using another actor.

I can include my crash logs if necessary.
If you know the solution, please answer :slight_smile:

Use

NewObject<YourClass>();

to create instances of classes outside of a constructor.
delete will not work, you would have to use the destruction method of the object you create for that. Something like

YourObject.Destroy();

But that wont work in your case. The code you have written creates a particle component and IMMEDIATELY destroys it again. It can’t do anything, because it is destroyed before it is even rendered. You don’t need to destroy it, the garbage collector will do that for you.

Also: does this line trace work propperly? because the end location should be something like

startLocation + GetActorForwardVector() * length;

The end location has to depend on the start location if you just want to shoot forward, yours currently doesnt do that.

Thank you very, very much! :slight_smile:

There is a method in UGameplayStatics called SpawnEmitterAtLocation which wraps up all the particle creation, activation, and fun stuff for you. I’d just switch to that.

Also these functions are already there for spawning an emitter super easy:

UGameplayStatics::SpawnEmitterAtLocation

UGameplayStatics::SpawnEmitterAttached

Oh, thank you :).