x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

UE4 Crashes when a Line Trace won't hit anything.

Hello.
I made a weapon that fires using Line Trace. It works well, but every time it doesn't hit anything it crashes my engine.
Here's my Line Trace 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 = StartLocation + PlayerCamera->GetForwardVector() * 100000000.0f;
     
         if (World && ShootDelay <= 0.0f)
         {
             World->LineTraceSingleByChannel(FireHit, StartLocation, EndLocation, ECC_Pawn, FireTraceParams);
     
             UGameplayStatics::SpawnEmitterAtLocation(World, ProjectileParticleSystem, FireHit.Location);
     
             ImpactVector = EndLocation / 10000.0f;
             if (FireHit.GetComponent()->IsSimulatingPhysics()) FireHit.GetComponent()->AddImpulseAtLocation(ImpactVector, FireHit.ImpactPoint);
             ShootDelay = 30.0f;
     
             /*DrawDebugLine(
                 GetWorld(),
                 StartLocation,
                 FireHit.Location,
                 FColor(255, 0, 0),
                 false, -1, 0,
                 12.333
             );*/
         }


If you know why, then please answer.

Product Version: UE 4.15
Tags:
more ▼

asked Mar 14 '17 at 12:57 PM in C++ Programming

avatar image

J0K3R_12QQ
94 21 24 28

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

LineTraceSingleByChannel returns true if it hit something and false otherwise. Right now you're just blindly assuming it hits something and then calling "FireHit.GetComponent()" which will return null if it didn't hit anything. You need to change your code so it's something like this:

 If (World->LineTraceSingleByChannel(....))
 {
   // The trace hit something. It's now safe to use the result. 
 }
more ▼

answered Mar 14 '17 at 01:04 PM

avatar image

ExtraLifeMatt
1.6k 34 14 48

avatar image J0K3R_12QQ Mar 14 '17 at 01:05 PM

Thank you :)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question