Unreal Engineの動きの定義は使いやすいBluePrintが主流と思われるが、C++で実装したいので、tutorialをやってみた。tutorialのままなのだが、作った画面は以下
実際の画面では、机の上のキューブが上下に動きながら、回転します。
ソースコードは以下
file: FloatingActor.h
#pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "FloatingActor.generated.h" UCLASS() class TEST_CS02_API AFloatingActor : public AActor { GENERATED_BODY() public: // Sets default values for this actor's properties AFloatingActor(); UPROPERTY(VisibleAnywhere) UStaticMeshComponent* VisualMesh; protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; };
file: FloatingActor.cpp
#include "FloatingActor.h" // Sets default values AFloatingActor::AFloatingActor() { // 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; VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh")); VisualMesh -> SetupAttachment(RootComponent); static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube")); if(CubeVisualAsset.Succeeded()) { VisualMesh->SetStaticMesh(CubeVisualAsset.Object); VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f)); } } // Called when the game starts or when spawned void AFloatingActor::BeginPlay() { Super::BeginPlay(); } // Called every frame void AFloatingActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); FVector NewLocation = GetActorLocation(); FRotator NewRotation = GetActorRotation(); float RunningTime = GetGameTimeSinceCreation(); float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime)); NewLocation.Z += DeltaHeight* 40.0f; float DeltaRotation = DeltaTime * 80.0f; NewRotation.Yaw += DeltaRotation; SetActorLocationAndRotation(NewLocation, NewRotation); }
■ご参考URL
docs.unrealengine.com
■今後の課題
Cubeを次々に発生させたい。そして後方に飛んでいかせたい。アクタを生成するのはSpawnActor関数らしい。これをタイマを使って連続的に発生させると、Cubeが生成されて流れていくように作れるはずなのだが
C++からSPAWNさせる方法(どこに書いたらいいのやら)
FString path = "/Game/SomeActor.SomeActor_C"; TSubclassOf<class AActor> ClassObject = TSoftClassPtr<AActor>(FSoftObjectPath(*PathName)).LoadSynchronous(); if (ClassObject != nullptr) { ASomeActor* actor = (ASomeActor*)GetWorld()->SpawnActor<AActor>(ClassObject); } [https://papersloth.hatenablog.com/entry/2018/09/24/155634:title]
引用:
Unreal C++/Blueprint 雑多なメモ (Unreal Engine 4.26.1) - Qiita