[C++] How do I scale down mesh or scale up collider correctly?

Here’s what it looks like when I constructed them by default:

And here’s what it should look like in the editor when I changed the mesh’s scale size to 0.5 for each axis. Note that I did this in “Simulate” mode in the UE4 Editor.

38108-ue4_ball2.png

I don’t know how to scale down the mesh, or scale up the USphereComponent by 50% of its original scale value.

This is how it looks in my code:

Obstacle.cpp

// Sets default values
AObstacle::AObstacle(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer.SetDefaultSubobjectClass<UMovementComponent>(TEXT("ObstacleBasicMovement")))
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	this->TimerCountdown = this->TimerInitialValue = 3;

	//Uniform scale
	FVector Scale = FVector(1.0f);

	//Setting basic mesh and collider
	this->SphereCollider = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("SphereCollider"));
	//this->SphereCollider->SetRelativeScale3D(Scale * 2.0f);
	this->SphereCollider->SetWorldScale3D(Scale * 1.5f);
	this->SphereCollider->SetRelativeLocation(FVector::ZeroVector);
    this->SphereCollider->SetCollisionProfileName(TEXT("Pawn"));
	this->SphereCollider->SetSimulatePhysics(true);
	this->SphereCollider->SetSphereRadius(38.5f);
	this->SetRootComponent(this->SphereCollider);


	static ConstructorHelpers::FObjectFinder<UStaticMesh> BodyMeshObject(TEXT("StaticMesh'/Game/Meshes/Sphere_Mesh.Sphere_Mesh'"));
	if (BodyMeshObject.Succeeded()){
		this->BodyMesh = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("BodyMesh"));
		this->BodyMesh->SetStaticMesh(BodyMeshObject.Object);
		this->BodyMesh->SetRelativeLocation(FVector::ZeroVector);
		this->BodyMesh->AttachTo(this->SphereCollider);
	}

	//Other properties
	this->Acceleration = FVector::ZeroVector;
	this->Velocity = FVector::ZeroVector;
}

I had tried to use SetRelativeScale3D() and SetWorldScale3D(), but it doesn’t seem to scale my components, even when restarting UE4.

What am I doing wrong? Thanks in advance.

You code is in the class constructor, mainly this sets the “default” value for your object when you put it in your scene. SetWorldScale3D and SetRelativeScale3D will both work, if you use them inside BeginPlay() or OnConstruction() function.You can override these like so:

virtual void BeginPlay() override;
virtual void OnConstruction(const FTransform& Transform) override;

The BeginPlay() function will be called when the game starts. The OnConstruction() function is called whenever you change a property to the object. This include in the editor, so if you want this to be applied while in the editor, you can do that there.

So, for example, you can do that to do your scaling :

void AObstacle::BeginPlay()
{
         //Uniform scale
         FVector Scale = FVector(1.0f);
	SphereCollider->SetWorldScale3D(Scale * 1.5f);
}

//or you can do that:

void AObstacle::OnConstruction(const FTransform& Transform)
{
	if (SphereCollider)
	{
		   FVector Scale = FVector(1.0f);
                  //Here you could replace your hardcoded 1.5f for a public variable exposed in the editor...
	           SphereCollider->SetWorldScale3D(Scale * 1.5f);
	}
}

Also, you don’t need to use “this->” everywhere, it’s not necessary.

The this-> is a habit of mine dating back to Java programming where it was encouraged to use it mostly for referencing objects.

Thank you for your answer.