MoveComponentTo c++ doesn't work

Hi everyone,
So I have a little problem and I don’t really know how to figure out.
I try to move a bullet I spaw, with my character to a target. I have do it in Blueprint and it’s work really good but I don’t know it’s not working in C++.

void ABullet::Reach_target()
{
	if (target_position != FVector(0, 0, 0))
	{
		FVector destination;
		destination = FMath::Lerp(GetActorLocation(), target_position, 0.05);
		FLatentActionInfo Looll;
		UKismetSystemLibrary::MoveComponentTo(GetMesh(), target_position, FRotator::ZeroRotator, false, false, 5.0f, EMoveComponentAction::Move, Looll);
		if (GetActorLocation() == target_position)
		{
			this->Destroy(true);
			UE_LOG(LogTemp, Warning, TEXT("MEURTTTTTTT"));

		}
		//GetWorldTimerManager().SetTimer(Timer, this, &ABullet::Reach_target, 0.1f, false);
	}
}

I am sure the target is valid because i print it and it’s good, and I am sure too my fonction “Reach_target” is call and i go into the if, so it’s the function who doesn’t work i guess ?

I include in my cpp the #include “Kismet/KismetSystemLibrary.h”
What am I doing wrong ?

Hello Palarra,

I’m not familiar with using this function so I’ll be taking a look at this but I wanted to ask; Is the bullet a component on your character or something attached to the character? Also, are you intending to move the entire bullet or just the Static Mesh component of the bullet? The MoveComponentTo function, as the name suggests, will only move a component and not the entire Actor’s location.

Hey,
Not my bullet is not attached to my character it’s just my character who spawn it with spawnactorofclass and like you see in the code i make the Getmesh() so i only want to move my static mesh so I really don’ know why it’s notworking :’(

I found out how to make it work. You need to set up one variable in the FLatentActionInfo variable like so:

LatentAction.CallbackTarget = this;

This is assuming that “this” is referring to the Bullet mesh’s parent actor. After doing this, it should move just like the Blueprint node does.

Hope this helps!

1 Like

Ohh ty mannnnnnnn !!! :smiley:

I ran into this exact problem, and I’m so glad I found this thread. What a hero!