最简单的 UE 4 C++ 教程 —— 给 Actor 添加径向推力【十八】

举报
ShaderJoy 发表于 2021/11/18 22:52:54 2021/11/18
【摘要】 原教程是基于 UE 4.18,我是基于 UE 4.25】 英文原地址 接上一节教程,本教程非常有趣,我们将通过在设定范围内的所有物体上添加径向推力来模拟爆炸。 创建一个名为 AddRadialForce 的新角色。我们不需要对头文件做任何操作。下面是由虚幻生成的默认头文件。 AddRadialForce.h #pra...

原教程是基于 UE 4.18,我是基于 UE 4.25】

英文原地址

接上一节教程,本教程非常有趣,我们将通过在设定范围内的所有物体上添加径向推力来模拟爆炸。

创建一个名为 AddRadialForce 的新角色。我们不需要对头文件做任何操作。下面是由虚幻生成的默认头文件。

AddRadialForce.h


  
  1. #pragma once
  2. #include "CoreMinimal.h"
  3. #include "GameFramework/Actor.h"
  4. #include "AddRadialForce.generated.h"
  5. UCLASS()
  6. class UNREALCPP_API AAddRadialForce : public AActor
  7. {
  8. GENERATED_BODY()
  9. public:
  10. // Sets default values for this actor's properties
  11. AAddRadialForce();
  12. protected:
  13. // Called when the game starts or when spawned
  14. virtual void BeginPlay() override;
  15. public:
  16. // Called every frame
  17. virtual void Tick(float DeltaTime) override;
  18. };

为了在调试过程中提供帮助,让我们将 DrawDebugHelpers 头文件添加到代码中。


  
  1. #include "AddRadialForce.h"
  2. // add debug helpfers
  3. #include "DrawDebugHelpers.h"

在这个例子中,我们将在 BeginPlay() 函数中执行所有的逻辑。我们想要收集我们范围内的所有命中结果,并从范围的扫描中获得结果。为此,我们将使用 TArray 跟踪重叠的 actor。


  
  1. void AAddRadialForce::BeginPlay()
  2. {
  3. Super::BeginPlay();
  4. // create tarray for hit results
  5. TArray<FHitResult> OutHits;
  6. // crate tarray for sweep actors
  7. TArray<AActor*> SweepActors;
  8. }

接下来我们要声明 TArray 数组 OutHits 。我们希望扫描范围从 actor 的位置开始同时也从该位置结束,并让 CollisionShape 是一个 500 单位的球体。您可以使用 GetActorLocation() 来获取 actor 的位置,它返回一个向量。我们使用 FCollisionShape:: makephere (500.0f) 来创建一个CollisionShape


  
  1. // Called when the game starts or when spawned
  2. void AAddRadialForce::BeginPlay()
  3. {
  4. Super::BeginPlay();
  5. // create tarray for hit results
  6. TArray<FHitResult> OutHits;
  7. // get actor locations
  8. FVector MyLocation = GetActorLocation();
  9. // start and end locations. The sphere will create the radial sweep.
  10. FVector Start = MyLocation;
  11. FVector End = MyLocation;
  12. // create a collision sphere
  13. FCollisionShape MyColSphere = FCollisionShape::MakeSphere(500.0f);
  14. }

为了可视化扫描的球体,我们将绘制一个调试球体。


  
  1. // Called when the game starts or when spawned
  2. void AAddRadialForce::BeginPlay()
  3. {
  4. Super::BeginPlay();
  5. // create tarray for hit results
  6. TArray<FHitResult> OutHits;
  7. // get actor locations
  8. FVector MyLocation = GetActorLocation();
  9. // start and end locations. The sphere will create the radial sweep.
  10. FVector Start = MyLocation;
  11. FVector End = MyLocation;
  12. // create a collision sphere
  13. FCollisionShape MyColSphere = FCollisionShape::MakeSphere(500.0f);
  14. // draw collision sphere
  15. DrawDebugSphere(GetWorld(), GetActorLocation(), MyColSphere.GetSphereRadius(), 50, FColor::Cyan, true);
  16. }

接下来我们要检查我们的 actor 在我们 BeginPlay() 时是否击中了任何东西。每个 actor 都有GetWorld 功能。从 ,我们将使用从 GetWorld() 函数得到的 SweepMultiByChannel() 函数并对其设置上面刚创建的变量作为参数。这将返回一个 bool,指示是否有其他 actor 在该 actor 的范围内。


  
  1. void AAddRadialForce::BeginPlay()
  2. {
  3. Super::BeginPlay();
  4. // create tarray for hit results
  5. TArray<FHitResult> OutHits;
  6. // get actor locations
  7. FVector MyLocation = GetActorLocation();
  8. // start and end locations. The sphere will create the radial sweep.
  9. FVector Start = MyLocation;
  10. FVector End = MyLocation;
  11. // create a collision sphere
  12. FCollisionShape MyColSphere = FCollisionShape::MakeSphere(500.0f);
  13. // draw collision sphere
  14. DrawDebugSphere(GetWorld(), GetActorLocation(), MyColSphere.GetSphereRadius(), 50, FColor::Cyan, true);
  15. // check if something got hit in the sweep
  16. bool isHit = GetWorld()->SweepMultiByChannel(OutHits, Start, End, FQuat::Identity, ECC_WorldStatic, MyColSphere);
  17. }

如果 isHit 为真,我们将通过遍历数组 OutHits ,并向每个成功 cast 的 actor 的根组件添加径向推力。


  
  1. if (isHit)
  2. {
  3. // loop through TArray
  4. for (auto& Hit : OutHits)
  5. {
  6. UStaticMeshComponent* MeshComp = Cast<UStaticMeshComponent>((Hit.GetActor())->GetRootComponent());
  7. if (MeshComp)
  8. {
  9. // alternivly you can use ERadialImpulseFalloff::RIF_Linear for the impulse to get linearly weaker as it gets further from origin.
  10. // set the float radius to 500 and the float strength to 2000.
  11. MeshComp->AddRadialImpulse(GetActorLocation(), 500.f, 2000.f, ERadialImpulseFalloff::RIF_Constant, true);
  12. }
  13. }
  14. }

最终完整 cpp 代码如下


  
  1. #include "AddRadialForce.h"
  2. // add debug helpfers
  3. #include "DrawDebugHelpers.h"
  4. // Sets default values
  5. AAddRadialForce::AAddRadialForce()
  6. {
  7. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  8. PrimaryActorTick.bCanEverTick = true;
  9. }
  10. // Called when the game starts or when spawned
  11. void AAddRadialForce::BeginPlay()
  12. {
  13. Super::BeginPlay();
  14. // create tarray for hit results
  15. TArray<FHitResult> OutHits;
  16. // get actor locations
  17. FVector MyLocation = GetActorLocation();
  18. // start and end locations. The sphere will create the radial sweep.
  19. FVector Start = MyLocation;
  20. FVector End = MyLocation;
  21. // create a collision sphere
  22. FCollisionShape MyColSphere = FCollisionShape::MakeSphere(500.0f);
  23. // draw collision sphere
  24. DrawDebugSphere(GetWorld(), GetActorLocation(), MyColSphere.GetSphereRadius(), 50, FColor::Cyan, true);
  25. // check if something got hit in the sweep
  26. bool isHit = GetWorld()->SweepMultiByChannel(OutHits, Start, End, FQuat::Identity, ECC_WorldStatic, MyColSphere);
  27. if (isHit)
  28. {
  29. // loop through TArray
  30. for (auto& Hit : OutHits)
  31. {
  32. UStaticMeshComponent* MeshComp = Cast<UStaticMeshComponent>((Hit.GetActor())->GetRootComponent());
  33. if (MeshComp)
  34. {
  35. // alternivly you can use ERadialImpulseFalloff::RIF_Linear for the impulse to get linearly weaker as it gets further from origin.
  36. // set the float radius to 500 and the float strength to 2000.
  37. MeshComp->AddRadialImpulse(GetActorLocation(), 500.f, 2000.f, ERadialImpulseFalloff::RIF_Constant, true);
  38. }
  39. }
  40. }
  41. }
  42. // Called every frame
  43. void AAddRadialForce::Tick(float DeltaTime)
  44. {
  45. Super::Tick(DeltaTime);
  46. }

实际运行起来的效果图如下

文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。

原文链接:panda1234lee.blog.csdn.net/article/details/119140900

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。