UE4 C++学习笔记之场景物品

任务:在场景中创建一个能与角色互动的金币,当角色与金币发生重叠时,金币消失并播放相应特效

第一步、以Actor为基类,新建C++类Item

Item.h代码如下:

#pragma once

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

UCLASS()
class LEARNTEST_API AItem : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AItem();

        //声明球体组件作为类的根结点
	UPROPERTY(VisibleAnywhere)
	class USphereComponent* Sphere;

        //声明静态网格体
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item|Mesh ")
	class UStaticMeshComponent* StaticMesh;

        //声明粒子系统组件
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "Item|Particle ")
	class UParticleSystemComponent* ParticleComp;

        //声明例子特效
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item|Particle ")
	class UParticleSystem* ParticleOverlap;

        //声明声音提示特效
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item|Sound ")
	class USoundCue* SoundOverlap;

        //声明函数:发生重叠时调用
	UFUNCTION()
	void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

        //声明函数:结束重叠时调用
	UFUNCTION()
	void OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

Item.cpp代码如下:

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


#include "Item.h"
#include "Components\SphereComponent.h"
#include "Kismet\GameplayStatics.h"
#include "Components\StaticMeshComponent.h"
#include "Particles\ParticleSystemComponent.h"
#include "Sound\SoundCue.h"

#define _FUNCTION_ "AItem::OnOverlapEnd"
#define _function_ "AItem::OnOverlapBegin"

// 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;

        //创建球体组件,并将其设定为根组件
	Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere"));
	RootComponent = Sphere;

        //创建静态网格体,并将其附着在根组件上
	StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StatciMesh"));
	StaticMesh->SetupAttachment(RootComponent);

        //创建粒子系统组件,并将其附着在根组件上
	ParticleComp = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("ParticComp"));
	ParticleComp->SetupAttachment(RootComponent);
}

void AItem::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    	//当发生重叠事件时,在物品位置处播放粒子特效
	if (ParticleOverlap)
	{
		UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ParticleOverlap, GetActorLocation());
	}

        //当发生重叠事件时,播放声音提示特效
	if (SoundOverlap)
	{
		UGameplayStatics::PlaySound2D(GetWorld(), SoundOverlap);
	}

        //自毁
	Destroy();
}

void AItem::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
        
}

// Called when the game starts or when spawned
void AItem::BeginPlay()
{
	Super::BeginPlay();

        //绑定重叠函数
	Sphere->OnComponentBeginOverlap.AddDynamic(this, &AItem::OnOverlapBegin);
	Sphere->OnComponentEndOverlap.AddDynamic(this, &AItem::OnOverlapEnd);
}

// Called every frame
void AItem::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

第二步、以创建的Item类为基类生成相应的蓝图类,将其命名为MyCoin,并在蓝图类中编辑相关静态网格体和特效等

然后将该物品拖到场景中就能与角色互动了。

猜你喜欢

转载自blog.csdn.net/weixin_44928892/article/details/108170712