7
7
#include " Engine/Public/TimerManager.h"
8
8
#include " GameFramework/CharacterMovementComponent.h"
9
9
#include " Engine/Classes/Camera/CameraComponent.h"
10
+ #include " Engine/Classes/Kismet/GameplayStatics.h"
10
11
#include " EngineUtils.h"
11
12
12
13
// Sets default values for this component's properties
@@ -36,15 +37,15 @@ void UTargetSystemComponent::BeginPlay()
36
37
if (!CharacterReference)
37
38
{
38
39
UE_LOG (LogTemp, Error, TEXT (" [%s] TargetSystemComponent: Cannot get Owner reference ..." ), *this ->GetName ());
40
+ return ;
39
41
}
40
42
41
- CharacterController = CharacterReference-> GetInstigatorController ( );
42
- if (!CharacterController )
43
+ PlayerController = UGameplayStatics::GetPlayerController ( GetWorld (), 0 );
44
+ if (!PlayerController )
43
45
{
44
- UE_LOG (LogTemp, Error, TEXT (" [%s] TargetSystemComponent: Cannot get Controller reference ..." ), *CharacterReference->GetName ());
46
+ UE_LOG (LogTemp, Error, TEXT (" [%s] TargetSystemComponent: Cannot get PlayerController reference ..." ), *CharacterReference->GetName ());
47
+ return ;
45
48
}
46
-
47
- PlayerController = Cast<APlayerController>(CharacterController);
48
49
}
49
50
50
51
void UTargetSystemComponent::TickComponent (float DeltaTime, ELevelTick TickType, FActorComponentTickFunction * ThisTickFunction)
@@ -53,6 +54,12 @@ void UTargetSystemComponent::TickComponent(float DeltaTime, ELevelTick TickType,
53
54
54
55
if (TargetLocked && NearestTarget)
55
56
{
57
+ if (!TargetIsTargetable (NearestTarget))
58
+ {
59
+ TargetLockOff ();
60
+ return ;
61
+ }
62
+
56
63
SetControlRotationOnTarget (NearestTarget);
57
64
58
65
// Target Locked Off based on Distance
@@ -66,7 +73,8 @@ void UTargetSystemComponent::TickComponent(float DeltaTime, ELevelTick TickType,
66
73
if (BreakLineOfSightDelay <= 0 )
67
74
{
68
75
TargetLockOff ();
69
- } else
76
+ }
77
+ else
70
78
{
71
79
bIsBreakingLineOfSight = true ;
72
80
GetWorld ()->GetTimerManager ().SetTimer (
@@ -87,7 +95,8 @@ void UTargetSystemComponent::TargetActor()
87
95
if (TargetLocked)
88
96
{
89
97
TargetLockOff ();
90
- } else
98
+ }
99
+ else
91
100
{
92
101
TArray<AActor*> Actors = GetAllActorsOfClass (TargetableActors);
93
102
NearestTarget = FindNearestTarget (Actors);
@@ -114,7 +123,7 @@ void UTargetSystemComponent::TargetActorWithAxisInput(float AxisValue)
114
123
{
115
124
return ;
116
125
}
117
-
126
+
118
127
// Lock off target
119
128
AActor* CurrentTarget = NearestTarget;
120
129
@@ -127,7 +136,7 @@ void UTargetSystemComponent::TargetActorWithAxisInput(float AxisValue)
127
136
128
137
// Get All Actors of Class
129
138
TArray<AActor*> Actors = GetAllActorsOfClass (TargetableActors);
130
-
139
+
131
140
// For each of these actors, check line trace and ignore Current Target and build the list of actors to look from
132
141
TArray<AActor*> ActorsToLook;
133
142
@@ -236,7 +245,7 @@ void UTargetSystemComponent::TargetLockOn(AActor* TargetToLockOn)
236
245
ControlRotation (true );
237
246
}
238
247
239
- CharacterController ->SetIgnoreLookInput (true );
248
+ PlayerController ->SetIgnoreLookInput (true );
240
249
241
250
OnTargetLockedOn.Broadcast (TargetToLockOn);
242
251
}
@@ -257,7 +266,7 @@ void UTargetSystemComponent::TargetLockOff()
257
266
ControlRotation (false );
258
267
}
259
268
260
- CharacterController ->ResetIgnoreLookInput ();
269
+ PlayerController ->ResetIgnoreLookInput ();
261
270
262
271
OnTargetLockedOff.Broadcast (NearestTarget);
263
272
}
@@ -271,7 +280,8 @@ void UTargetSystemComponent::CreateAndAttachTargetLockedOnWidgetComponent(AActor
271
280
if (TargetLockedOnWidgetClass)
272
281
{
273
282
TargetLockedOnWidgetComponent->SetWidgetClass (TargetLockedOnWidgetClass);
274
- } else
283
+ }
284
+ else
275
285
{
276
286
TargetLockedOnWidgetComponent->SetWidgetClass (UTargetSystemLockOnWidget::StaticClass ());
277
287
}
@@ -290,15 +300,8 @@ TArray<AActor*> UTargetSystemComponent::GetAllActorsOfClass(TSubclassOf<AActor>
290
300
for (TActorIterator<AActor> ActorIterator (GetWorld (), ActorClass); ActorIterator; ++ActorIterator)
291
301
{
292
302
AActor* Actor = *ActorIterator;
293
- bool bIsImplemented = Actor->GetClass ()->ImplementsInterface (UTargetSystemTargetableInterface::StaticClass ());
294
- if (bIsImplemented)
295
- {
296
- bool bIsTargetable = ITargetSystemTargetableInterface::Execute_IsTargetable (Actor);
297
- if (bIsTargetable)
298
- {
299
- Actors.Add (Actor);
300
- }
301
- } else
303
+ bool IsTargetable = TargetIsTargetable (Actor);
304
+ if (IsTargetable)
302
305
{
303
306
Actors.Add (Actor);
304
307
}
@@ -307,6 +310,17 @@ TArray<AActor*> UTargetSystemComponent::GetAllActorsOfClass(TSubclassOf<AActor>
307
310
return Actors;
308
311
}
309
312
313
+ bool UTargetSystemComponent::TargetIsTargetable (AActor* Actor)
314
+ {
315
+ bool bIsImplemented = Actor->GetClass ()->ImplementsInterface (UTargetSystemTargetableInterface::StaticClass ());
316
+ if (bIsImplemented)
317
+ {
318
+ return ITargetSystemTargetableInterface::Execute_IsTargetable (Actor);
319
+ }
320
+
321
+ return true ;
322
+ }
323
+
310
324
AActor* UTargetSystemComponent::FindNearestTarget (TArray<AActor*> Actors)
311
325
{
312
326
TArray<AActor*> ActorsHit;
@@ -380,7 +394,7 @@ bool UTargetSystemComponent::LineTrace(FHitResult& HitResult, AActor* OtherActor
380
394
381
395
FRotator UTargetSystemComponent::GetControlRotationOnTarget (AActor* OtherActor)
382
396
{
383
- FRotator ControlRotation = CharacterController ->GetControlRotation ();
397
+ FRotator ControlRotation = PlayerController ->GetControlRotation ();
384
398
385
399
FVector CharacterLocation = CharacterReference->GetActorLocation ();
386
400
FVector OtherActorLocation = OtherActor->GetActorLocation ();
@@ -395,12 +409,12 @@ FRotator UTargetSystemComponent::GetControlRotationOnTarget(AActor* OtherActor)
395
409
396
410
void UTargetSystemComponent::SetControlRotationOnTarget (AActor* TargetActor)
397
411
{
398
- if (!CharacterController )
412
+ if (!PlayerController )
399
413
{
400
414
return ;
401
415
}
402
416
403
- CharacterController ->SetControlRotation (GetControlRotationOnTarget (TargetActor));
417
+ PlayerController ->SetControlRotation (GetControlRotationOnTarget (TargetActor));
404
418
}
405
419
406
420
float UTargetSystemComponent::GetDistanceFromCharacter (AActor* OtherActor)
0 commit comments