IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> UE4创建可以交互的Pickup效果和声音 -> 正文阅读

[游戏开发]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

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-10-22 21:51:25  更:2022-10-22 21:52:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年1日历 -2025/1/17 5:54:40-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码