赞
踩
本节主要是解决当炮弹打中物体后所发生的事情。
- 1 FScriptDelegate delegates;
- 2
- 3 UFUNCTION()
- 4 void OnHits(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
自定义一个OnHits函数,用来处理炮击到物体后的事情。因为要用到代理,且是动态的,所以,需要加上UFUNCTION反射标记。这个方法与蓝图中下图方法一样。
设置炮弹的基本属性
- 1 RootComponent = ball;
- 2 ball->SetWorldScale3D(FVector(5.0f));
- 3 hitEffect->SetupAttachment(ball);
- 4 ball->SetNotifyRigidBodyCollision(true); //刚体碰撞开启
- 5 ball->SetGenerateOverlapEvents(true);
添加代理事件
- 1 void AMissle::BeginPlay()
- 2 {
- 3 Super::BeginPlay();
- 4 delegates.BindUFunction(this, FName("OnHits"));
- 5 ball->OnComponentHit.Add(delegates);
- 6
- 7 }
- 1 void AMissle::OnHits(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
- 2 {
- 3 AStaticMeshActor* target = Cast<AStaticMeshActor>(OtherActor);
- 4 if (target) {
- 5 UGameplayStatics::PlaySoundAtLocation(GetWorld(), hitSound, HitComponent->GetRelativeLocation());
- 6 hitEffect->Activate(true);
- 7 ball->SetSimulatePhysics(true);
- 8 HitComponent->AddImpulseAtLocation(NormalImpulse,HitComponent->GetRelativeLocation());
- 9 ball->SetNotifyRigidBodyCollision(false);
- 10 }
- 11
- 12 float initTime = FPlatformTime::Seconds();
- 13 if (FPlatformTime::Seconds() - initTime >= 1.0f) {
- 14 Destroy();
- 15 }
- 16
- 17 }
炮弹击中物体的时候,开启物理属性,对击中的物体施加冲击力,之后,关闭刚体。
本节无图例。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。