首页 > 其他分享 >UE 实现黑洞效果

UE 实现黑洞效果

时间:2022-12-10 23:45:23浏览次数:38  
标签:RangeSphere 效果 BlackHole 黑洞 DestroySphere UPrimitiveComponent UE BlackHoleMesh A

UE 黑洞效果制作

注意:所吸取的物体必须开启 模拟物理生成重叠事件 两个选项

创建一个Actor的C++类,名称为BlackHole。

为类头文件添加两个SphereComponent和一个StaticMesh

UPROPERTY(EditDefaultsOnly, Category = "BlackHole")
USphereComponent* DestroySphere;

UPROPERTY(EditDefaultsOnly, Category = "BlackHole")
USphereComponent* RangeSphere;

UPROPERTY(EditDefaultsOnly, Category = "BlackHole")
UStaticMeshComponent* BlackHoleMesh;

在构造函数中初始化成员

//初始化成员
DestroySphere = CreateDefaultSubobject<USphereComponent>(TEXT("BlackHole"));
//设置DestroySphere碰撞为仅查询
DestroySphere->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
//设置球体半径为32.0f
DestroySphere->SetSphereRadius(32.0f);

RangeSphere = CreateDefaultSubobject<USphereComponent>(TEXT("RangeBlackHole"));
RangeSphere->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
RangeSphere->SetSphereRadius(3000.0f);

BlackHoleMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BlackHoleMesh"));
//设置BlackHoleMesh碰撞为无碰撞
BlackHoleMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);

//设置根组件为BlackHoleMesh
RootComponent = BlackHoleMesh;
//将DestroySphere、RangeSphere添加到BlackHoleMesh下
DestroySphere->SetupAttachment(BlackHoleMesh);
RangeSphere->SetupAttachment(BlackHoleMesh);

//==============添加DestroySphere重叠的触发方法==============
DestroySphere->OnComponentBeginOverlap.AddDynamic(this, &ABlackHole::DestroyOnOverlapBegin);

添加DestroySphere重叠的触发方法

声明:

//重叠方法的参数列表必须一致,且必须添加UFUNCTION()
UFUNCTION()
void DestroyOnOverlapBegin(class UPrimitiveComponent* HitComp,
    class AActor* OtherActor, class UPrimitiveComponent* OtherComp,
    int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

实现:

void ABlackHole::DestroyOnOverlapBegin(UPrimitiveComponent* HitComp, 
	AActor* OtherActor, UPrimitiveComponent* OtherComp, 
	int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	OtherActor->Destroy();
}

将函数添加至DestroySphere中

//将函数添加至DestroySphere的重叠触发方法
DestroySphere->OnComponentBeginOverlap.AddDynamic(this, &ABlackHole::DestroyOnOverlapBegin);

为RangeSphere范围内的所有物体添加力

void ABlackHole::Tick(float DeltaTime)
{
        Super::Tick(DeltaTime);
	TSet<UPrimitiveComponent*> OverlapObjects;
        //获取组件重叠范围内的所有对象,将信息保存至TSet中
	GetOverlappingComponents(OverlapObjects);
	for (UPrimitiveComponent* overlapObject : OverlapObjects)
	{
		//此处bAccelChange属性为true则该力为加速力忽略物理力,即质量将不产生作用
		overlapObject->AddRadialForce(GetActorLocation(),RangeSphere->GetScaledSphereRadius(),
			-1500.0f,ERadialImpulseFalloff::RIF_MAX,true);

	}
}

AddRadialForce属性信息

Parameter Description 中文翻译
Origin Origin of force in world space. 在世界空间中力的起源
Radius Radius within which to apply the force. 施加力的半径
Strength Strength of force to apply. 施加力的强度
Falloff Allows you to control the strength of the force as a function of distance from Origin. 允许你控制力的强度作为距离原点的函数。
bAccelChange If true, Strength is taken as a change in acceleration instead of a physical force (i.e. mass will have no effect). 如果为真,则强度被视为加速度的变化,而不是物理力(即质量将没有影响)。

完成后你的代码应该如下:

BlackHole.h

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

#pragma once

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

class USphereComponent;

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

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

	UPROPERTY(EditDefaultsOnly, Category = "BlackHole")
	USphereComponent* DestroySphere;
	UPROPERTY(EditDefaultsOnly, Category = "BlackHole")
	USphereComponent* RangeSphere;
	UPROPERTY(EditDefaultsOnly, Category = "BlackHole")
	UStaticMeshComponent* BlackHoleMesh;
	
	UFUNCTION()
	void DestroyOnOverlapBegin(class UPrimitiveComponent* HitComp,
		class AActor* OtherActor, class UPrimitiveComponent* OtherComp,
		int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
};

BlackHole.cpp

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

#include "BlackHole.h"
#include <Components/SphereComponent.h>

// Sets default values
ABlackHole::ABlackHole()
{
 	// 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;

	//初始化成员
	DestroySphere = CreateDefaultSubobject<USphereComponent>(TEXT("BlackHole"));
	//设置DestroySphere碰撞为仅查询
	DestroySphere->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
	//设置球体半径为32.0f
	DestroySphere->SetSphereRadius(32.0f);

	RangeSphere = CreateDefaultSubobject<USphereComponent>(TEXT("RangeBlackHole"));
	RangeSphere->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
	RangeSphere->SetSphereRadius(3000.0f);

	BlackHoleMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BlackHoleMesh"));
    //设置BlackHoleMesh碰撞为无碰撞
	BlackHoleMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
    
	//设置根组件为BlackHoleMesh
	RootComponent = BlackHoleMesh;
	//将DestroySphere、RangeSphere添加到BlackHoleMesh下
	DestroySphere->SetupAttachment(BlackHoleMesh);
	RangeSphere->SetupAttachment(BlackHoleMesh);

	//==============添加DestroySphere重叠的触发方法==============
	DestroySphere->OnComponentBeginOverlap.AddDynamic(this, &ABlackHole::DestroyOnOverlapBegin);
}

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

void ABlackHole::DestroyOnOverlapBegin(UPrimitiveComponent* HitComp,
	AActor* OtherActor, UPrimitiveComponent* OtherComp,
	int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	OtherActor->Destroy();
}

// Called every frame
void ABlackHole::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	TSet<UPrimitiveComponent*> OverlapObjects;
	//获取组件重叠范围内的所有对象,将信息保存至TSet中
	GetOverlappingComponents(OverlapObjects);
	for (UPrimitiveComponent* overlapObject : OverlapObjects)
	{
		//此处bAccelChange属性为true则该力为加速力忽略物理力,即质量将不产生作用
		overlapObject->AddRadialForce(GetActorLocation(), RangeSphere->GetScaledSphereRadius(),
			-1500.0f, ERadialImpulseFalloff::RIF_MAX, true);

	}
}

标签:RangeSphere,效果,BlackHole,黑洞,DestroySphere,UPrimitiveComponent,UE,BlackHoleMesh,A
From: https://www.cnblogs.com/L-TT/p/16972639.html

相关文章