Make gun weapon fire to the corner of the screen rather than diagonally

This commit is contained in:
baz 2024-08-02 01:45:48 +01:00
parent 64dc8ec7d1
commit 850ebb973e
1 changed files with 33 additions and 5 deletions

View File

@ -3,6 +3,11 @@
#include "GunWeapon.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetMathLibrary.h"
#include "vampires/PlayerCharacter.h"
#include "vampires/VampirePlayerController.h"
AGunWeapon::AGunWeapon()
{
}
@ -18,6 +23,29 @@ void AGunWeapon::FireWeaponAction_Implementation()
if (IsValid(ProjectileTemplate))
{
FVector2d ViewportSize;
GEngine->GameViewport->GetViewportSize(ViewportSize);
APlayerCharacter* PlayerCharacter = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
AVampirePlayerController* PlayerController = Cast<AVampirePlayerController>(
UGameplayStatics::GetPlayerController(PlayerCharacter, 0));
FVector TopLeft, TopLeftDir;
FVector TopRight, TopRightDir;
FVector BottomLeft, BottomLeftDir;
FVector BottomRight, BottomRightDir;
PlayerController->DeprojectScreenPositionToWorld(0, 0, TopLeft, TopLeftDir);
PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X, 0, TopRight, TopRightDir);
PlayerController->DeprojectScreenPositionToWorld(0, ViewportSize.Y, BottomLeft, BottomLeftDir);
PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X, ViewportSize.Y, BottomRight, BottomRightDir);
FVector actorLocation = GetActorLocation();
TopLeft.Z = actorLocation.Z;
TopRight.Z = actorLocation.Z;
BottomLeft.Z = actorLocation.Z;
BottomRight.Z = actorLocation.Z;
FActorSpawnParameters actorSpawnParameters;
actorSpawnParameters.Owner = this;
actorSpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
@ -25,18 +53,18 @@ void AGunWeapon::FireWeaponAction_Implementation()
AProjectile* projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters);
projectile->TargetDirection = FVector(1.0f, 1.0f, 0.0f);
projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopLeft);
projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters);
projectile->TargetDirection = FVector(-1.0f, 1.0f, 0.0f);
projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, TopRight);
projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters);
projectile->TargetDirection = FVector(1.0f, -1.0f, 0.0f);
projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, BottomLeft);
projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters);
projectile->TargetDirection = FVector(-1.0f, -1.0f, 0.0f);
projectile->TargetDirection = UKismetMathLibrary::GetDirectionUnitVector(actorLocation, BottomRight);
}
}