/
SAction_ProjectileAttack.cpp
90 lines (64 loc) · 3.05 KB
/
SAction_ProjectileAttack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Fill out your copyright notice in the Description page of Project Settings.
#include "SAction_ProjectileAttack.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundBase.h"
#include "GameFramework/Character.h"
USAction_ProjectileAttack::USAction_ProjectileAttack()
{
HandSocketName = "Muzzle_01";
AttackAnimDelay = 0.2f;
SweepRadius = 20.0f;
SweepDistanceFallback = 5000;
}
void USAction_ProjectileAttack::StartAction_Implementation(AActor* Instigator)
{
Super::StartAction_Implementation(Instigator);
ACharacter* Character = Cast<ACharacter>(Instigator);
if (Character)
{
Character->PlayAnimMontage(AttackAnim);
UGameplayStatics::SpawnEmitterAttached(CastingEffect, Character->GetMesh(), HandSocketName, FVector::ZeroVector, FRotator::ZeroRotator, EAttachLocation::SnapToTarget);
UGameplayStatics::SpawnSoundAttached(CastingSound, Character->GetMesh());
if (Character->HasAuthority())
{
FTimerHandle TimerHandle_AttackDelay;
FTimerDelegate Delegate;
Delegate.BindUFunction(this, "AttackDelay_Elapsed", Character);
GetWorld()->GetTimerManager().SetTimer(TimerHandle_AttackDelay, Delegate, AttackAnimDelay, false);
}
}
}
void USAction_ProjectileAttack::AttackDelay_Elapsed(ACharacter* InstigatorCharacter)
{
if (ensureAlways(ProjectileClass))
{
FVector HandLocation = InstigatorCharacter->GetMesh()->GetSocketLocation(HandSocketName);
// We trace against the environment first to find whats under the player crosshair.
// We use the hit location to adjust the projectile launch direction so it will hit what is under the crosshair rather than shoot straight forward from the player hands.
FActorSpawnParameters SpawnParams;
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
SpawnParams.Instigator = InstigatorCharacter;
FCollisionShape Shape;
Shape.SetSphere(SweepRadius);
// Ignore Player
FCollisionQueryParams Params;
Params.AddIgnoredActor(InstigatorCharacter);
FVector TraceDirection = InstigatorCharacter->GetControlRotation().Vector();
// Add sweep radius onto start to avoid the sphere clipping into floor/walls the camera is directly against.
FVector TraceStart = InstigatorCharacter->GetPawnViewLocation() + (TraceDirection * SweepRadius);
// endpoint far into the look-at distance (not too far, still adjust somewhat towards crosshair on a miss)
FVector TraceEnd = TraceStart + (TraceDirection * SweepDistanceFallback);
FHitResult Hit;
// returns true if we got to a blocking hit (Channel1="Projectile" defined in DefaultGame.ini)
if (GetWorld()->SweepSingleByChannel(Hit, TraceStart, TraceEnd, FQuat::Identity, ECC_GameTraceChannel1, Shape, Params))
{
// Overwrite trace end with impact point in world
TraceEnd = Hit.ImpactPoint;
}
// find new direction/rotation from Hand pointing to impact point in world.
FRotator ProjRotation = (TraceEnd - HandLocation).Rotation();
FTransform SpawnTM = FTransform(ProjRotation, HandLocation);
GetWorld()->SpawnActor<AActor>(ProjectileClass, SpawnTM, SpawnParams);
}
StopAction(InstigatorCharacter);
}