C++での開発を勉強しており、アクタの作成とシーン上での配置まではできました。
しかし、作成したアクタのピボットが想定より下にずれてしまっており、原因と解決策を探しています。
(追加したArrowと比較すると分かりやすいです)
大変お手数をおかけいたしますが、どなたかお力添えいただけますと幸いです。
▽作成したコード
// ヘッダーファイル
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Components/ArrowComponent.h" //これはOK
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CPPComponent.generated.h"
UCLASS()
class CPP_BP_API ACPPComponent : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACPPComponent();
// Scene Component(シーンコンポーネント)を追加
// DefaultSceneRootの部分が変数名
UPROPERTY(EditAnywhere)
TObjectPtr<USceneComponent> DefaultSceneRoot;
// StaticMesh Componentを追加
UPROPERTY(EditAnywhere) //Detailで表示する為に必要
TObjectPtr<UStaticMeshComponent> StaticMesh; // TObjectPtrはポインタの代わりらしい
// Arrow Componentを追加
UPROPERTY(VisibleAnywhere)
TObjectPtr<UArrowComponent> Arrow;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
// PrintString関数のDurationに設定する変数
const float Duration = 10.0f;
// PrintString関数のTextColorに設定する変数
const FLinearColor Textcolor = FLinearColor(0.0, 0.66, 1.0);
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// cppファイル
// Fill out your copyright notice in the Description page of Project Settings.
#include "CPPComponent.h"
#include "Kismet/KismetSystemLibrary.h"
#include "Components/ArrowComponent.h"
// Sets default values
// コンストラクタ
ACPPComponent::ACPPComponent()
{
// 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;
// SceneCopmonentを作成する
DefaultSceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComponent"));
// SceneComponetをRootComponentに設定する
RootComponent = DefaultSceneRoot;
// StaticMeshComponentを作成する
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
// StaticMeshをLoadしてStaticMeshComponentに設定する
UStaticMesh* Mesh = LoadObject<UStaticMesh>(NULL, TEXT("/Game/CPP_BP/Meshes/SM_SampleCube"), NULL, LOAD_None, NULL);
StaticMesh->SetStaticMesh(Mesh);
// StaticMeshComponentをRootComponentにAttachする
StaticMesh->SetupAttachment(RootComponent);
// ArrowComponentを作成する
Arrow = CreateDefaultSubobject<UArrowComponent>(TEXT("ArrowComponent"));
// ArrrowComponentの位置を設定する
Arrow->SetRelativeLocation(FVector(50.0f, 0.0f, 0.0f));
// ArrowComponentをStaticMeshComponentにAttachする
Arrow->SetupAttachment(StaticMesh);
}
// Called when the game starts or when spawned
void ACPPComponent::BeginPlay()
{
Super::BeginPlay();
// 変数を作成する
FString Message = "C++ Hello World!";
// PrintStringノードと同じ処理
// UKsimetSystemLibraryクラスのPrintString関数を呼び出す
UKismetSystemLibrary::PrintString(
this
, Message
, true
, true
, Textcolor
, Duration
, TEXT("None")
);
}
// Called every frame
void ACPPComponent::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}