当前位置:   article > 正文

基于C++代码的UE4学习(四十七)——为坦克开火的炮弹添加碰撞代理事件_unreal fplatformtime

unreal fplatformtime

 

本节主要是解决当炮弹打中物体后所发生的事情。

 

  1. 1 FScriptDelegate delegates;
  2. 2
  3. 3 UFUNCTION()
  4. 4 void OnHits(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

自定义一个OnHits函数,用来处理炮击到物体后的事情。因为要用到代理,且是动态的,所以,需要加上UFUNCTION反射标记。这个方法与蓝图中下图方法一样。

 

设置炮弹的基本属性

  1. 1 RootComponent = ball;
  2. 2 ball->SetWorldScale3D(FVector(5.0f));
  3. 3 hitEffect->SetupAttachment(ball);
  4. 4 ball->SetNotifyRigidBodyCollision(true); //刚体碰撞开启
  5. 5 ball->SetGenerateOverlapEvents(true);

添加代理事件

 

 

  1. 1 void AMissle::BeginPlay()
  2. 2 {
  3. 3 Super::BeginPlay();
  4. 4 delegates.BindUFunction(this, FName("OnHits"));
  5. 5 ball->OnComponentHit.Add(delegates);
  6. 6
  7. 7 }

 

 

  1. 1 void AMissle::OnHits(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
  2. 2 {
  3. 3 AStaticMeshActor* target = Cast<AStaticMeshActor>(OtherActor);
  4. 4 if (target) {
  5. 5 UGameplayStatics::PlaySoundAtLocation(GetWorld(), hitSound, HitComponent->GetRelativeLocation());
  6. 6 hitEffect->Activate(true);
  7. 7 ball->SetSimulatePhysics(true);
  8. 8 HitComponent->AddImpulseAtLocation(NormalImpulse,HitComponent->GetRelativeLocation());
  9. 9 ball->SetNotifyRigidBodyCollision(false);
  10. 10 }
  11. 11
  12. 12 float initTime = FPlatformTime::Seconds();
  13. 13 if (FPlatformTime::Seconds() - initTime >= 1.0f) {
  14. 14 Destroy();
  15. 15 }
  16. 16
  17. 17 }

 

炮弹击中物体的时候,开启物理属性,对击中的物体施加冲击力,之后,关闭刚体。

 

本节无图例。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/662303
推荐阅读
相关标签
  

闽ICP备14008679号