blaster/Source/Blaster/Weapon/ProjectileWeapon.cpp

43 lines
1.1 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "ProjectileWeapon.h"
#include "Projectile.h"
#include "Engine/SkeletalMeshSocket.h"
void AProjectileWeapon::Fire(const FVector& HitTarget)
{
Super::Fire(HitTarget);
if (!HasAuthority()) return;
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
);
}
}
}
}