在实际场景中通常会有各种交互,当角色碰到炸弹或者金币的时候,通常会发生各种爆炸收到损伤或者获取一定金钱值,并且会发出一定的声音,到达非常逼真的效果。 首先,分析一下场景发现,无论角色遇到的炸弹或者金币,都有共同的属性。比如:都有碰撞网格、静态网格、粒子效果、播放声音等。所以基于以上分析我们可以新建一个基础的C++类Item。然后炸弹和金币分别继承这个基类,并且可以扩展出自己的特殊的行为。
一.创建子类Item
在UEEditor中创建类 

#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Item.generated.h"
UCLASS()
class FIRSTPROJECT_API AItem : public AActor {
GENERATED_BODY()
public:
AItem();
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Item|Collision")
class USphereComponent* collision;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Item|Mess")
class UStaticMeshComponent* mess;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item|Mess")
UParticleSystemComponent* idleParticle;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item|Mess")
UParticleSystem* overlapPartcile;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item|Sound")
class USoundCue* overlapSound;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item|Property")
bool bRotate = false;
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
UFUNCTION()
virtual void onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent*OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult&SweepResult);
UFUNCTION()
virtual void onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
};
#include "Item.h"
#include "Components/SphereComponent.h"
#include "Particles/ParticleSystemComponent.h"
#include "Engine/World.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
AItem::AItem() {
PrimaryActorTick.bCanEverTick = true;
collision = CreateDefaultSubobject<USphereComponent>(TEXT("Collision"));
RootComponent = collision;
mess = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mess"));
mess->SetupAttachment(GetRootComponent());
idleParticle = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("IdleParticle"));
idleParticle->SetupAttachment(GetRootComponent());
}
void AItem::BeginPlay() {
Super::BeginPlay();
collision->OnComponentBeginOverlap.AddDynamic(this, &AItem::onBeginOverlap);
collision->OnComponentEndOverlap.AddDynamic(this, &AItem::onEndOverlap);
}
void AItem::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
if (bRotate){
FRotator rotator = GetActorRotation();
rotator.Yaw += (DeltaTime * 45.0);
SetActorRotation(rotator);
}
}
void AItem::onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult&SweepResult) {
UE_LOG(LogTemp, Warning, TEXT("AItem::onBeginOverlap"));
if (overlapPartcile){
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), overlapPartcile, GetActorLocation(), FRotator(0.0), true);
}
if (overlapSound){
UGameplayStatics::PlaySound2D(this, overlapSound);
}
Destroy();
}
void AItem::onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {
UE_LOG(LogTemp, Warning, TEXT("AItem::onEndOverlap"));
}
二.创建子类AExplosive和APickup

APickup类
#pragma once
#include "CoreMinimal.h"
#include "Item.h"
#include "Pickup.generated.h"
UCLASS()
class FIRSTPROJECT_API APickup : public AItem
{
GENERATED_BODY()
public:
APickup();
void onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)override;
void onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)override;
};
#include "Pickup.h"
APickup::APickup() {
}
void APickup::onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
Super::onBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
UE_LOG(LogTemp, Warning, TEXT("APickup::onBeginOverlap"));
}
void APickup::onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {
Super::onEndOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);
UE_LOG(LogTemp, Warning, TEXT("APickup::onEndOverlap"));
}
AExplosive类
#pragma once
#include "CoreMinimal.h"
#include "Item.h"
#include "Explosive.generated.h"
UCLASS()
class FIRSTPROJECT_API AExplosive : public AItem {
GENERATED_BODY()
public:
AExplosive();
void onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)override;
void onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)override;
};
#include "Explosive.h"
AExplosive::AExplosive() {
}
void AExplosive::onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
Super::onBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
UE_LOG(LogTemp, Warning, TEXT("AExplosive::onBeginOverlap"));
}
void AExplosive::onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {
Super::onEndOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);
UE_LOG(LogTemp, Warning, TEXT("AExplosive::onEndOverlap"));
}
三.导入声音
新建文件夹Sound ,右键导入资源  导入声音资源后,创建Cue  
四、创建子类对应蓝图类Pickup_BP和Explosive_BP
首先创建:Explosive_BP 
 
首先创建:Pichup_BP   
五.把创建好的子类蓝图拖动到场景中
 点击【运行】当角色触碰到金币或者炸弹的时候,就会出现相应的粒子效果或者声音。
aaa
|