1.同理导入导弹和大陆(都不导入骨骼)
2.点击大陆的网格体设置碰撞
3.然后把大地拖入游戏场景
========================
1.创建导弹类Missle和敌人类Enemy,都继承于actor类
2.为 Missle添加一个静态网格体组件和运动组件
- UPROPERTY(VisibleAnywhere)
- UStaticMeshComponent* Mesh;
-
- UPROPERTY(VisibleAnywhere)
- class UProjectileMovementComponent * MovementComponent;
3.在源文件中引入头文件
- #include "Components/StaticMeshComponent.h"
- #include "GameFramework/ProjectileMovementComponent.h"
4.在构造函数中初始化
- //创建静态网格体
- Mesh = CreateDefaultSubobject
(TEXT("Mesh")); - //静态网格体设为根组件
- RootComponent = Mesh;
- //创建移动组件
- MovementComponent = CreateDefaultSubobject
(TEXT("MovementComponent")); - //设置初始速度
- MovementComponent->InitialSpeed = 500.0f;
5.在头文件中声明一个函数,用来作为重叠事件
- //OverlappedComponent:自身的重叠组件 OtherActor:重叠的对方 OtherComp:对方的组件
- UFUNCTION()
- void OverlapHanler(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
6.此时我们想增加一个逻辑,打中一个飞艇就加分,首先我们在gamemode里面,添加分数
- UCLASS()
- class TUTDRONE_API ATutDroneGameModeBase : public AGameModeBase
- {
- GENERATED_BODY()
- public:
- //本次游戏得分
- UPROPERTY()
- int32 num;
-
- };
7.在源文件中进行定义
- void AMissle::OverlapHanler(UPrimitiveComponent * OverlappedComponent, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
- {
- //首先判断,碰撞到的物体是否继承于AEnemy类
- if (OtherActor->IsA
()) - {
- //该物体销毁
- OtherActor->Destroy();
- //加分
- ATutDroneGameModeBase * gm = Cast
(GetWorld()->GetAuthGameMode()); - if (gm != nullptr)
- {
- gm->num++;
- }
-
- //自身也要炸掉
- this->Destroy();
- }
- }
8.在构造函数中将改函数绑定到重叠开始事件
- //启动overlap事件
- Mesh->SetGenerateOverlapEvents(true);
- //设置重叠开始事件
- Mesh->OnComponentBeginOverlap.AddDynamic(this,&AMissle::OverlapHanler)
============================
1.为 Enemy添加一个静态网格体组件并在源文件中创建,步骤同上
2.在头文件中,声明一个函数,作为销毁时的事件
- UFUNCTION()
- void DestroyHandler(AActor * DestroyedActor);
3.在源文件中进行定义(未完)
4.在构造函数中激活重叠事件并绑定销毁事件
- //启动overlap事件
- Mesh->SetGenerateOverlapEvents(true);
- this->OnDestroyed.AddDynamic(this, &AEnemy::DestroyHandler);
================
1.创建对应的蓝图
2.把两个mesh的碰撞预设都设为这个
3.给胡萝卜模型添加一个胶囊体碰撞
4.给飞艇自动生成一个碰撞
在这里设置相关参数
测试
当重叠事件发生时,打印
UKismetSystemLibrary::PrintString(this, TEXT("BANGBANGBANG"));
敌人消失并打印出改字符串
========================
导入音频
1.创建音频的cue
2.为声音添加一个衰减,随着距离衰减
3.为飞艇添加粒子特效和声音
- //爆炸特效
- UPROPERTY(EditAnywhere)
- class UPartycleSystem * Explosion;
- //爆炸声音
- UPROPERTY(EditAnywhere)
- class USoundBase * ExplosionSound;
4.在源文件中引入相关文件
#include "Kismet/GamePlayStatics.h"
5.在销毁事件函数中生成特效和声音
- void AEnemy::DestroyHandler(AActor * DestroyedActor)
- {
- UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), Explosion, this->GetTransform());
- UGameplayStatics::PlaySoundAtLocation(this, ExplosionSound, this->GetActorLocation(),2.0f);
- }
6.在胡萝卜头文件里,添加一个声音
- //发射声音
- UPROPERTY(EditAnywhere)
- class USoundBase * FireSound;
7.在源文件的构造函数中发出声音
- // Called when the game starts or when spawned
- void AMissle::BeginPlay()
- {
- Super::BeginPlay();
- UGameplayStatics::PlaySoundAtLocation(this, FireSound, this->GetActorLocation(), 2.0f);
- }
8.在蓝图中选择声音
测试:
除了没有爆炸特效,其他都正常。