当前位置: 首页 > news >正文

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

相关文章:

  • 网站集约化建设的目的/视频营销的策略与方法
  • wordpress等待加载动画设置/广告公司怎么找客户资源
  • 宜城网站建设/seo网络优化公司哪家好
  • 网站建设公司做销售好不好/开网站需要什么流程
  • 网站建设实习目的/揭阳市seo上词外包
  • 怎样可以查到做网站公司/项目营销策划方案
  • go语言代码练习
  • Python 多进程处理数据
  • 线程池7个参数详解
  • 高项 23 项目管理成熟度模型
  • 牛客网之SQL刷题练习——一个实用的网站
  • ts 类型学习
  • PHP基于thinkphp+vue共享单车系统 nodejs前后端分离
  • ubuntu 20.04 qemu linux6.0.1 制作ext4根文件系统
  • 面试总结day3:springboot多环境配置、如何优雅的停止线程、gateway作用应用场景
  • MyBatisPlus之多数据源
  • Hive的表操作3
  • 【Java版oj】移除链表元素