Actor won't move at all

I have a Background Block Actor that is always moving to the left in the background of the screen. It consists of a static mesh and a decal with the background image. I am trying to move it with a projectile movement component, but it just stays in place and doesn’t want to move. What am I missing?

UCLASS()
class MYGAME_API ABackgroundBlock : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ABackgroundBlock();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	UPROPERTY(EditAnywhere)
	class UStaticMeshComponent* mesh = nullptr;
	UPROPERTY(EditAnywhere)
	class UDecalComponent* decal = nullptr;
	UPROPERTY(EditAnywhere)
	class UProjectileMovementComponent* movement = nullptr;
	
	UPROPERTY(VisibleAnywhere)
	float speed = 400;

};
ABackgroundBlock::ABackgroundBlock()
{
 	// 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;

	movement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("MovementComp"));
	movement->ProjectileGravityScale = 0;
	movement->Velocity = FVector(0, 0, 0);

	mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshBottom"));
	mesh->OnComponentBeginOverlap.AddDynamic(this, &ABackgroundBlock::teleport);

	decal = CreateDefaultSubobject<UDecalComponent>(TEXT("Decal"));
	decal->SetupAttachment(mesh);
	
}

// Called when the game starts or when spawned
void ABackgroundBlock::BeginPlay()
{
        //movement->Velocity = FVector(0, -speed, 0);
	//movement->InitialSpeed = speed;
	//movement->MaxSpeed = movement->InitialSpeed;
}

Hey @boisbois2, the problem is that you are updating the initial speed after the ProjectileMovementComponent has already been created. What you need to do is move the code that you have in your BeginPlay function into your constructor. Like this:

ATestProjectileMovement::ATestProjectileMovement()
{
	// 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;

	MovementComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("MovementComp"));
	MovementComp->ProjectileGravityScale = 0;
	MovementComp->Velocity = FVector(0, -1, 0);
	MovementComp->InitialSpeed = speed;
	MovementComp->MaxSpeed = MovementComp->InitialSpeed;

	MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshBottom"));
	//MeshComp->OnComponentBeginOverlap.AddDynamic(this, &ATestProjectileMovement::Teleport);

	DecalComp = CreateDefaultSubobject<UDecalComponent>(TEXT("Decal"));
	DecalComp->SetupAttachment(MeshComp);

}

You should be good to go with this. Hope this helps, good luck.

Thanks for the reply, but the actor still doesn’t move. Does the code you sent work properly on your end and if so can you please show the whole file?

Edit: Nevermind, it turns out my class was just broken. I created a new class and did the initialization the way you did and it worked. Thanks a lot!

1 Like