blaster/Source/Blaster/Weapon/HitScanWeapon.cpp

65 lines
1.5 KiB
C++
Raw Normal View History

2022-05-19 17:13:21 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "HitScanWeapon.h"
#include "Blaster/Character/BlasterCharacter.h"
#include "Engine/SkeletalMeshSocket.h"
#include "Kismet/GameplayStatics.h"
void AHitScanWeapon::Fire(const FVector& HitTarget)
{
Super::Fire(HitTarget);
APawn* OwnerPawn = Cast<APawn>(GetOwner());
if (OwnerPawn == nullptr) return;
AController* InstigatorController = OwnerPawn->GetController();
const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName("MuzzleFlash");
if (MuzzleFlashSocket && InstigatorController)
{
FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh());
FVector Start = SocketTransform.GetLocation();
FVector End = Start + (HitTarget - Start) * 1.25f;
FHitResult FireHit;
UWorld* World = GetWorld();
if (World)
{
World->LineTraceSingleByChannel(
FireHit,
Start,
End,
ECC_Visibility
);
if (FireHit.bBlockingHit)
{
ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(FireHit.GetActor());
if (BlasterCharacter)
{
if (HasAuthority())
{
UGameplayStatics::ApplyDamage(
BlasterCharacter,
Damage,
InstigatorController,
this,
UDamageType::StaticClass()
);
}
}
if (ImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(
World,
ImpactParticles,
FireHit.ImpactPoint,
FireHit.ImpactNormal.Rotation()
);
}
}
}
}
}