LineTrace delay until HitLocation

Hello all,

I’m having trouble figuring out the logic on how to make LineTrace tracing from StartLocation to EndTLocation with delay. I want to make it like bullet flying from StartLocation to EndTLocation with float Speed for example but at the moment when my Fire() function is triggered my LineTrace is instant. Could someone assist me with the logic?

// HitResults array
TArray<FHitResult> HitResults;

// Get start and end traces
FVector StartLocation = PCharacter->GetSocketLocation(FName("BarrelSocket")); // Gets socket location on PCharacter
FVector EndTLocation = PAimingComponent->GetHitLocation(); // Gets location where crosshair is pointing at

// Setup collision query and collision respons parameters
FCollisionQueryParams* TraceParams = new FCollisionQueryParams();
TraceParams->bTraceComplex = false;
FCollisionResponseParams ResponseParams(ECollisionResponse::ECR_Overlap);

// Perform the LineTrace
GetWorld()->LineTraceMultiByChannel(HitResults, StartLocation, EndTLocation, ECC_Visibility, *TraceParams, ResponseParams);

// Draw Debug Line for testing purposes
DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor::Red, false, 5.f);

	for (int x = 0; x != HitResults.Num(); ++x)
	{
		// Check of we hit Solid Material. If we have we can stop processing hitresults
		AStaticMeshActor* PotentialSolidMaterial = Cast<AStaticMeshActor>(HitResults[x].Actor.Get());
		if (PotentialSolidMaterial != NULL && !PotentialSolidMaterial->IsPendingKill())
		{
			UE_LOG(LogTemp, Warning, TEXT("SolidMaterial Hit!"));
			break;
		}
	}

I’m not sure where the delay logic should be added but I suspect it should be for “FVector EndTLocation” with for loop?

Thank you in advance!

Regards,
Georgi.

Wouldn’t it be easier to just make a projectile object and let it control its own logic?
Or you could have the owner update the position of the projectile each frame (basically perform another LineTrace each frame).

Hello Akitsuki,

I’ll try doing the logic with two LineTraces right now.

Under Projectile Object do you mean creating Projectile Actor class and adding Projectile Movement to it? If this is the case I tried that with UGameplayStatics::SuggestProjectileVelocity() setting LaunchVelocity from StartLocation to HitLocation in LaunchSpeed ( UGameplayStatics::SuggestProjectileVelocity(this, LaunchVelocity, StartLocation, HitLocation, LaunchSpeed, false, 0, 0, ESuggestProjVelocityTraceOption::DoNotTrace) ) but the problem was in my projectile spawn:

GetWorld()->SpawnActor<AProjectile>(BP_Projectile, PCharacter->GetSocketLocation(FName("BarrelSocket")), PCharacter->GetSocketRotation(FName("BarrelSocket")));
Projectile->LaunchProjectile(LaunchSpeed);

My BarrelSocket is not attached to a component that is elevating and it was not working as I wanted to. I tried adding logic so that it end up exactly on my crosshair but it was getting under it. When I managed to hardcode the logic it was working in long range shooting but ended up on the ground when shooting at close range. So I kept trying but didn’t manage to make it work. While I was digging the internet to find any solutions I saw that many people suggest using LineTrace instead because it doesn’t ruin the performance and it is much easier to implement.

So I went back to my code and did it with the LineTrace method because I’m new in Unreal with some knowledge in C++ but never had any practice with it. I only have few C++ courses finished and jumped into Unreal :slight_smile:

Also LineTrace was meeting my requirements - “Keep it simple”

I agree with you - the author should create projectile and the problem will be solved.

Ok I managed to do this with projectile object.

Thank you very much for you help Akitsuki Rin!! :slight_smile:

No problem! Have fun!