I’m making a 2D game and I want to make something like this with line traces:
And that’s what’s happening so far:
And that’s what I’ve written so far:
FCollisionQueryParams TraceParams = FCollisionQueryParams(FName(TEXT("RV_Trace")), true, this);
TraceParams.bTraceComplex = true;
TraceParams.bTraceAsyncScene = true;
TraceParams.bReturnPhysicalMaterial = true;
TraceParams.AddIgnoredActor(this);
FVector Start = GetActorLocation();
FHitResult Hit(ForceInit);
float angle = 90; // will be modifiable
const float ang = angle / 2;
uint8 divisions = 18; // will be modifiable as well
const float step = angle / divisions;
float range = 500; // that one too
for (int8 i = ang*-1; i <= ang; i += step) {
float h = sin(i) * range;
float s = cos(i) * range;
if (Controller->GetControlRotation() == FRotator(0, 180, 0))
s *= -1;
FVector End = FVector(Start.X+s, Start.Y, Start.Z+h);
bool didHit = GetWorld()->LineTraceSingle(Hit, Start, End, ECC_Visibility, TraceParams);
DrawDebugLine(this->GetWorld(), Start, Hit.TraceEnd, FColor::Red, true, 1000, 10);
if (didHit) {
ABasePawn* Bp = Cast<ABasePawn>(Hit.GetActor());
if (Bp != NULL)
Bp->TakeDamage(20);
break;
}
}
I don’t want to have any aiming mechanics just yet so just ignore the gun.