【UnrealEngine】円運動をするアクタをC++で作成する
C++で円運動をするアクタを作成してみたので、コードを残しておこうと思います。
ヘッダーファイル
変数を作成しておく
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TimeTestActor.generated.h"
UCLASS()
class CREATEACTOR_API ATimeTestActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATimeTestActor();
FVector StartLocation;
float RunTime;
UPROPERTY(EditAnywhere, Category="Move")
float MoveSpeed = 1.0f;
UPROPERTY(EditAnywhere, Category = "Move")
float MoveRadius = 200.0f;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
ソースファイル
SinとCosで円運動をさせる
// Fill out your copyright notice in the Description page of Project Settings.
#include "TimeTestActor.h"
// Sets default values
ATimeTestActor::ATimeTestActor()
{
// 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;
}
// Called when the game starts or when spawned
void ATimeTestActor::BeginPlay()
{
Super::BeginPlay();
StartLocation = GetActorLocation();
RunTime = 0.0f;
}
// Called every frame
void ATimeTestActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
RunTime += DeltaTime;
float X = FMath::Sin(RunTime * MoveSpeed) * MoveRadius;
float Y = FMath::Cos(RunTime * MoveSpeed) * MoveRadius;
FVector NewLocation = StartLocation;
NewLocation.X += X;
NewLocation.Y += Y;
SetActorLocation(NewLocation);
}
上記コードを記述しゲーム内に配置すると動画と同様の円運動をするアクタを作成できたはずです。
SinとCosが何をしているのかを理解できる円運動を作成するのはそこまで難しくないなという印象でした!
軸を変更すると回る方向を調整することもできます。
NewLocation変数のX,YをY,Zに変更するだけです。
◆YouTube
ゆうtuber・ゆうくん – YouTube
