I am currently trying to move a blueprint into c++ code that takes the position of where the bullet will be fired from and fires it towards where the player is looking.
My code for shooting is as follows:
void ABaseWeapon::Shoot(FVector Location, FVector ForwardVector, FRotator ControlRotation)
{
if (RoundsInMagazine > 0)
{
//Take one round from the magazine
RoundsInMagazine--;
//Line Trace By Channel
FCollisionQueryParams RV_TraceParams = FCollisionQueryParams(FName(TEXT("RV_Trace")), true, this);
RV_TraceParams.bTraceComplex = true;
RV_TraceParams.bTraceAsyncScene = true;
RV_TraceParams.bReturnPhysicalMaterial = false;
//Re-initialize hit info
FHitResult RV_Hit(ForceInit);
//Calculate tracing range
FVector EndPoint = Location + (ForwardVector * 10000);
GetWorld()->LineTraceSingle(
RV_Hit,
Location,
EndPoint,
ECC_Visibility,
RV_TraceParams);
//Did the line trace hit anything?
if (RV_Hit.bBlockingHit)
{
FActorSpawnParameters spawnParams;
spawnParams.bNoCollisionFail = true;
spawnParams.bNoFail = true;
//Create spawn location for projectile
const FVector SpawnLocation = Location + ControlRotation.RotateVector(GunOffset);
FRotator lookat = (SpawnLocation - RV_Hit.ImpactPoint).Rotation();
GetWorld()->SpawnActor<UObject>(ProjectileBlueprint->GeneratedClass, SpawnLocation, lookat, spawnParams);
UE_LOG(LogTemp, Warning, TEXT("Hit"));
}
else
{
FActorSpawnParameters spawnParams;
spawnParams.bNoCollisionFail = true;
spawnParams.bNoFail = true;
//Create spawn location for projectile
const FVector SpawnLocation = Location + ControlRotation.RotateVector(GunOffset);
GetWorld()->SpawnActor<UObject>(ProjectileBlueprint->GeneratedClass, SpawnLocation, ControlRotation, spawnParams);
UE_LOG(LogTemp, Warning, TEXT("No Hit"));
}
}
}
It seems to be logging correctly when I am pointing at something it should hit but I am seeing no projectile being spawned. When I do not get a hit result the projectile fires in the default direction that ends up parallel with the reticule in the centre of the screen.
I believe I am going wrong here:
FRotator lookat = (SpawnLocation - RV_Hit.ImpactPoint).Rotation();
GetWorld()->SpawnActor<UObject>(ProjectileBlueprint->GeneratedClass, SpawnLocation, lookat, spawnParams);
Thanks for any assistance.
tl;dr: I want my projectile to fire towards the location that LineTraceSingle() hits against.
EDIT: Also going to add an illustration of what I am wanting to do: