UE4创建可以交互的Pickup效果和声音

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

一.创建子类Item

在UEEditor中创建类
在这里插入图片描述

在这里插入图片描述

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Item.generated.h"

UCLASS()
class FIRSTPROJECT_API	AItem : public AActor {
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	AItem();

	//基础形状碰撞体
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Item|Collision")
	class USphereComponent* collision;

	//基础网格体
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Item|Mess")
	class UStaticMeshComponent* mess;

	//空闲时的粒子系统
	/*
	UParticleSystemComponent 和 UParticleSystem 的区别:
	UParticleSystemComponent需要调用CreateDefaultSubobject来创建,UParticleSystem不需要创建,直接可以在蓝图中被选中使用
	*/
	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:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	// Called every frame
	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);
};

// Fill out your copyright notice in the Description page of Project Settings.


#include "Item.h"
#include "Components/SphereComponent.h"
#include "Particles/ParticleSystemComponent.h"
#include "Engine/World.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"

// Sets default values
AItem::AItem() {
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	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());
}

// Called when the game starts or when spawned
void AItem::BeginPlay() {
	Super::BeginPlay();
	collision->OnComponentBeginOverlap.AddDynamic(this, &AItem::onBeginOverlap);
	collision->OnComponentEndOverlap.AddDynamic(this, &AItem::onEndOverlap);
}

// Called every frame
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类

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Item.h"
#include "Pickup.generated.h"

/**
 * 
 */
UCLASS()
class FIRSTPROJECT_API APickup : public AItem
{
	GENERATED_BODY()

public:
	APickup();

	//UFUNCTION() 在子类中不需要添加这个宏
	void onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)override;

	//UFUNCTION()
	void onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)override;
	
};

// Fill out your copyright notice in the Description page of Project Settings.


#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类

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Item.h"
#include "Explosive.generated.h"

/**
 *
 */
UCLASS()
class FIRSTPROJECT_API AExplosive : public AItem {
	GENERATED_BODY()

public:
	AExplosive();

	//UFUNCTION() 在子类中不需要添加这个宏
	void onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)override;

	//UFUNCTION()
	void onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)override;
};

// Fill out your copyright notice in the Description page of Project Settings.


#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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值