My component changes location on play

Hello!

I’ve made a class called Interactive Object in C++

The door blueprint class is a child of this.

So, the interactive object has two uproperty attributes, a skeletal mesh, and a hitbox (box collider).

My door blueprint in the blueprint viewport looks fine:

The hitbox is centered around the mesh, in the viewport editor, as it should

but then when i drag it into the editor:

Here’s my Interactive Object class:
Header:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "RSGameCharacter.h"
#include "InteractiveObject.generated.h"

UCLASS()
class RSGAME_API AInteractiveObject : public AActor
{
	GENERATED_BODY()

	ARSGameCharacter* nightguard;

public:	
	// Sets default values for this actor's properties
	AInteractiveObject();

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Collision")
	UBoxComponent* hitbox;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Mesh)
	USkeletalMeshComponent* meshcomp;

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	virtual void OnInteract();

	UFUNCTION()
	void BeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResultr);
	
	UFUNCTION()
	void EndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResultr);

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Interactivity")
	bool withinrange;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Interactivity")
	bool epressed;
	
};

Source:

// Fill out your copyright notice in the Description page of Project Settings.

#include "RSGame.h"
#include "InteractiveObject.h"
#include <typeinfo>


// Sets default values
AInteractiveObject::AInteractiveObject()
{
 	// 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;

	meshcomp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Skeletal Mesh"));
	meshcomp->CastShadow = true;
	RootComponent = meshcomp;

	hitbox = CreateDefaultSubobject<UBoxComponent>(TEXT("Hitbox"));
	hitbox->OnComponentBeginOverlap.AddDynamic(this, &AInteractiveObject::BeginOverlap);

	hitbox->SetRelativeLocation(meshcomp->GetComponentLocation());
}

// Called when the game starts or when spawned
void AInteractiveObject::BeginPlay()
{
	Super::BeginPlay();
	epressed = false;
	withinrange = false;
}

// Called every frame
void AInteractiveObject::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
	if (nightguard->IsValidLowLevel())
	{
		epressed = nightguard->InteractingCPP;
	}
	if (withinrange && epressed)
	{
		OnInteract();
		UE_LOG(LogTemp, Warning, TEXT("withinrange and epressed are true"));
	}
}

void AInteractiveObject::OnInteract()
{
	
}

void AInteractiveObject::BeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	UE_LOG(LogTemp, Warning, TEXT("Your message"));
	//if (typeid(OtherActor) == typeid(nightguard))
	//{
		UE_LOG(LogTemp, Warning, TEXT("Begin overlap"));
		nightguard = (ARSGameCharacter*) OtherActor;
		withinrange = true;
	//}
}
void AInteractiveObject::EndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	UE_LOG(LogTemp, Warning, TEXT("Your message"));
	//if (typeid(OtherActor) == typeid(nightguard))
	//{
		UE_LOG(LogTemp, Warning, TEXT("end overlap"));
		withinrange = false;
	//}
}

Sorry for the long post, I had trouble explaining my problem :S

I see that you are setting relative location by getting component location. You need to make the relative location 0, 0, 0. When you get component location you are passing the components X, Y, Z coordinates so your object will offset from the parent as it is not passing 0, 0, 0.

Also make sure blueprint that the child components location transform is not set to absolute.

+Russ Rockjaw

Hi!

This hasn’t worked. I am currently using FVector::ZeroVector in my relative location, and the problem persists :frowning: