forked from jerry-tom/unrealcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddRadialForce.cpp
More file actions
49 lines (38 loc) · 1.37 KB
/
AddRadialForce.cpp
File metadata and controls
49 lines (38 loc) · 1.37 KB
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
// Harrison McGuire
// UE4 Version 4.20.2
// https://github.com/Harrison1/unrealcpp
// https://severallevels.io
// https://harrisonmcguire.com
#include "AddRadialForce.h"
// add debug helpfers
#include "DrawDebugHelpers.h"
// Called when the game starts or when spawned
void AAddRadialForce::BeginPlay()
{
Super::BeginPlay();
// create tarray for hit results
TArray<FHitResult> OutHits;
// get actor locations
FVector MyLocation = GetActorLocation();
// create a collision sphere
FCollisionShape MyColSphere = FCollisionShape::MakeSphere(500.0f);
// draw collision sphere
DrawDebugSphere(GetWorld(), GetActorLocation(), MyColSphere.GetSphereRadius(), 50, FColor::Cyan, true);
// check if something got hit in the sweep
bool isHit = GetWorld()->SweepMultiByChannel(OutHits, MyLocation, MyLocation, FQuat::Identity, ECC_WorldStatic, MyColSphere);
if (isHit)
{
// loop through TArray
for (auto& Hit : OutHits)
{
UStaticMeshComponent* MeshComp = Cast<UStaticMeshComponent>((Hit.GetActor())->GetRootComponent());
if (MeshComp)
{
// alternivly you can use ERadialImpulseFalloff::RIF_Linear for the impulse to get linearly weaker as it gets further from origin.
// set the float radius to 500 and the float strength to 2000.
MeshComp->AddRadialImpulse(GetActorLocation(), 500.f, 2000.f, ERadialImpulseFalloff::RIF_Constant, true);
}
}
}
Destroy();
}