Thanks for your patience btw

I've narrowed down where that log is coming from. Bear with me...

So, printing V produces "-nan(ind)"

Code:

float V = (Sx / cos(Theta)) * FMath::Sqrt(Gravity / (2 * (Sx * tan(Theta) - Sz)));

I attempted to break down and log sections of the equation and these are the results:

Code:

UE_LOG(LogTemp, Warning, TEXT("V = %f"), (Gravity / (2 * (Sx * tan(Theta) - Sz))) );

This produces

LogTemp:Warning: Gravity / (2 * (Sx * tan(Theta)- Sz)) = -0.171647

Code:

UE_LOG(LogTemp, Warning, TEXT("V = %f"), FMath::Sqrt(Gravity / (2 * (Sx * tan(Theta) - Sz))));

So, when finding the Square root, I get the -nan(ind) message.

This suggests that something is wrong with the calculation of V, or one of the variables which are fed into it.

As for the current code block, it is as follows:

Code:

FVector TargetPosition = currentTargetRegion->GetRandomPointInVolume();
FRotator newrot = UKismetMathLibrary::FindLookAtRotation(startPos, TargetPosition);
AShell* Proj = World->SpawnActor<AShell>(ProjectileBP, startPos, FRotator(40, newrot.Yaw, 0), SpawnParams);
Proj->ProjectileComponent->SetVelocityInLocalSpace(FVector(0,0,0));
const float Gravity = World->GetGravityZ();
float Theta = (40 * PI / 180); // launch angle
FVector dir = TargetPosition - startPos; //direction
float Sz = dir.Z;// height difference
dir.Z = 0; // remove hight from direction
float Sx = dir.Size();// distance
float V = (Sx / cos(Theta)) * FMath::Sqrt(Gravity / (2 * (Sx * tan(Theta) - Sz)));
FVector VelocityOutput = FVector(V*cos(Theta), 0, V*sin(Theta));
Proj->ProjectileComponent->SetVelocityInLocalSpace(VelocityOutput);