blaster/Source/Blaster/Weapon/ProjectileWeapon.cpp

43 lines
1.1 KiB
C++
Raw Normal View History

2022-05-04 17:17:25 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "ProjectileWeapon.h"
2022-05-05 10:51:14 +00:00
#include "Projectile.h"
#include "Engine/SkeletalMeshSocket.h"
void AProjectileWeapon::Fire(const FVector& HitTarget)
{
Super::Fire(HitTarget);
2022-05-05 12:15:25 +00:00
if (!HasAuthority()) return;
2022-05-05 10:51:14 +00:00
APawn* InstigatorPawn = Cast<APawn>(GetOwner());
const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName(FName("MuzzleFlash"));
if (MuzzleFlashSocket)
{
FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh());
// From muzzle flash socket to hit location from TraceUnderCrosshairs
FVector ToTarget = HitTarget - SocketTransform.GetLocation();
FRotator TargetRotation = ToTarget.Rotation();
if (ProjectileClass && InstigatorPawn)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = GetOwner();
SpawnParams.Instigator = InstigatorPawn;
UWorld* World = GetWorld();
if (World)
{
World->SpawnActor<AProjectile>(
ProjectileClass,
SocketTransform.GetLocation(),
TargetRotation,
SpawnParams
);
}
}
}
}