Change shooting direction to use camera and centre of screen

This commit is contained in:
Louis Hobbs 2023-07-24 19:09:59 +01:00
parent 3ed84127c2
commit 9e7417eb59
1 changed files with 17 additions and 6 deletions

View File

@ -252,12 +252,26 @@ void APlayerCharacter::SetWalkingCallback(const FInputActionInstance& Instance)
void APlayerCharacter::CalculateHits(TArray<FHitResult>* hits) void APlayerCharacter::CalculateHits(TArray<FHitResult>* hits)
{ {
// Set up the collision query params, use the Weapon trace settings, Ignore the actor firing this trace
FCollisionQueryParams TraceParams(SCENE_QUERY_STAT(WeaponTrace), true, GetInstigator());
TraceParams.bReturnPhysicalMaterial = true;
// Set up randomness // Set up randomness
const int32 RandomSeed = FMath::Rand(); const int32 RandomSeed = FMath::Rand();
FRandomStream WeaponRandomStream(RandomSeed); FRandomStream WeaponRandomStream(RandomSeed);
const float Spread = CurrentWeapon->GetWeaponProperties()->WeaponSpread; const float Spread = CurrentWeapon->GetWeaponProperties()->WeaponSpread;
const float Range = CurrentWeapon->GetWeaponProperties()->ProjectileRange; const float Range = CurrentWeapon->GetWeaponProperties()->ProjectileRange;
FVector CamStart = CameraComponent->GetComponentTransform().GetLocation();
FVector CamRot = CameraComponent->GetComponentTransform().GetRotation().Vector();
FVector CamEnd = CamStart + CamRot * 99999.f;
FHitResult CamHit;
if (!GetWorld()->LineTraceSingleByChannel(CamHit, CamStart, CamEnd, ECC_Camera))
{
return;
}
// TODO: have this start from the end point of the weapon rather than character center // TODO: have this start from the end point of the weapon rather than character center
// Calculate starting position and direction // Calculate starting position and direction
FVector TraceStart; FVector TraceStart;
@ -265,16 +279,13 @@ void APlayerCharacter::CalculateHits(TArray<FHitResult>* hits)
GetController<APlayerController>()->GetPlayerViewPoint(TraceStart, PlayerRot); GetController<APlayerController>()->GetPlayerViewPoint(TraceStart, PlayerRot);
TraceStart = GetRootComponent()->GetComponentLocation(); TraceStart = GetRootComponent()->GetComponentLocation();
FVector AimDir = PlayerRot.Vector(); FVector AimDir = CamHit.ImpactPoint - TraceStart;
AimDir.Normalize();
TraceStart = TraceStart + AimDir * ((GetInstigator()->GetActorLocation() - TraceStart) | AimDir); TraceStart = TraceStart + AimDir * ((GetInstigator()->GetActorLocation() - TraceStart) | AimDir);
// Calculate the hit results from the trace // Calculate the hit results from the trace
TArray<FHitResult> HitResults; TArray<FHitResult> HitResults;
// Set up the collision query params, use the Weapon trace settings, Ignore the actor firing this trace
FCollisionQueryParams TraceParams(SCENE_QUERY_STAT(WeaponTrace), true, GetInstigator());
TraceParams.bReturnPhysicalMaterial = true;
for (size_t i = 0; i < CurrentWeapon->GetWeaponProperties()->ProjectilesPerShot; i++) for (size_t i = 0; i < CurrentWeapon->GetWeaponProperties()->ProjectilesPerShot; i++)
{ {
// Calculate the maximum distance the weapon can fire // Calculate the maximum distance the weapon can fire