C++で作成したアクタのピボット位置(アクタの中心点)がずれてしまう

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);

}


hello and welcome.
the pivot is defined on the mesh.
you could modify it with the editor mesh edit tools.

	// StaticMeshをLoadしてStaticMeshComponentに設定する
	UStaticMesh* Mesh = LoadObject<UStaticMesh>(NULL, TEXT("/Game/CPP_BP/Meshes/SM_SampleCube"), NULL, LOAD_None, NULL);
	StaticMesh->SetStaticMesh(Mesh);

it should be defined on that SM_SampleCube

ganbatte!

1 Like

Thank you for your response!
I see that the pivot position was dependent on the mesh… I had overlooked that.
However, the mesh itself has the pivot centered and only shifts down when assigned to the actor’s mesh, so I’ll look into it some more!
(Maybe it’s the root component…?)

That is strange.
i had issues where in blender the pivot seems to be correct, but then it’s not on unreal.

if the StaticMesh has a transform, then that could make a different.
for example StaticMesh->SetRelativeLocation…
but i don’t see that in your code.

an important thing (kiken!) is that when importing a mesh, there’s a setting to change the pivot or add an offset in the dialog.

Sorry for the delay in replying.
I had to restart the editor several times and it was fixed before I knew it.

I don’t know the root cause of the problem, but I will try to resolve it as a topic.
Thank you very much for your advice!

1 Like

omedetou :slight_smile:

1 Like