😖

【UE5】Chaos DestructionをC++で制御したい!

に公開

はじめに

きっかけ

Historia 様のブログでオブジェクトがかっこよくバラバラの粉々になるシーンを見て私も制御したいと思ったのですが、解説記事がBlueprintベースでC++をこよなく愛している(?)私にとっては少しわかりずらいです。今回はChaosの機能を遊びながら触れつつC++ Onlyで制作したいと思っています!!!

手順

まずは,ツールバーの[ツール]>[新規のC++]を押して、親クラスをAActorを選択し名前を決めます。



私の場合は、愚直に「ObstacleSystem」に名前を決めした。

そうすると、IDE(Visual Studio)などが自動で立ち上がると思います。

以下のように追加していきます。

BreakableObstacle.h
#pragma once

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

class AFieldSystemActor;            // 粉々起動装置
class UGeometryCollectionComponent; // 粉々になるメッシュ入れ


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

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

	UPROPERTY(EditDefaultsOnly)
	TObjectPtr<USceneComponent> DefaultSceneRoot;

	UPROPERTY(EditDefaultsOnly)
	TObjectPtr<UGeometryCollectionComponent> CrashableMesh;

    // Meshですよ
	UPROPERTY(EditAnywhere,Category = "Chaos")
	TSubclassOf<AFieldSystemActor> ChaosActor;

    // 当たったときの関数
	UFUNCTION()
	void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit);

private:
	bool bHit;

	void ClearFieldSystem();
	TObjectPtr<AActor> FieldSystems;
	float DestoryInterval = 2.0f;
	FTimerHandle CrashHandle;
};

BreakableObstacle.cpp
#include "../Actors/BreakableObstacle.h"
#include "UObject/ConstructorHelpers.h"
#include "Kismet/KismetSystemLibrary.h"
#include "Kismet/GameplayStatics.h"
#include "Field/FieldSystemActor.h"
#include "GeometryCollection/GeometryCollectionComponent.h"
#include "GameFramework/Character.h"

// Sets default values
ABreakableObstacle::ABreakableObstacle()
{
    // メッシュコンポーネントの設定
	CrashableMesh = CreateDefaultSubobject<UGeometryCollectionComponent>(TEXT("CrashMesh"));
	CrashableMesh->SetupAttachment(RootComponent);

    // メッシュを取得する
	ConstructorHelpers::FObjectFinder<UGeometryCollection> ObstacleMeshFinder(TEXT("/Script/GeometryCollectionEngine.GeometryCollection'/Game/Chaos/GC_BP_BreakableObstacle.GC_BP_BreakableObstacle'"));
	if (ObstacleMeshFinder.Succeeded())
	{
        // メッシュをくっつける
		CrashableMesh->SetRestCollection(ObstacleMeshFinder.Object);
	}

}

破壊したい場合は、FS_MasterFieldクラスをスポーンします.

BreakableObstacle.cpp
void ABreakableObstacle::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
	if (!bHit)
	{
		if (Cast<ACharacter>(OtherActor))
		{
			bHit = true;
			FVector HitLocation = Hit.Location;
			
			if (ChaosActor)
			{
				FTransform trans = FTransform(Hit.Location);
				FieldSystems = GetWorld()->SpawnActor<AActor>(ChaosActor, trans);

			}
		}
	}
}

この動作

キャラクターが動作したら1回だけバラバラになるようになっています。

課題点

Histria様のブログにも記載されているのですが、StartAwakeの設定がなぜかC++で動かすことができませんでした。関数自体見当たりませんでした。今後はそれも制御できるようにしたいと思っています。

参考文献

  • Historia [UE5]Chaos Destructionを使ってオブジェクトを破壊しよう、最終確認日:2025年8月25日

Discussion