How to move an actor in C++?

You can use UKismetMathLibrary::VInterpTo.
Do it in your Tick function assuming you precomputed your target location

I am currently using the SetActorLocation() function to move an actor(just a cube mesh) to a new position. This works well, but it just teleports the actor to a new location. I would like an animation where it slides to the new location over maybe a second or so. I was thinking I can use a loop to iterate over a timedelat and the SetActorLocation function. Would that be ok or is there a better way of doing it?

What is the best way of animating the movement of an actor to a new location in C++?


Are you using the tick function ?
You would do something like


        FVector CurrentLocation;
        float speed;


//in the main class function

           CurrentLocation = this->GetActorLocation(); // To save where ever the actor is in the viewport
            speed = 150.0f;   // change this to whatever

//in the tick function

        CurrentLocation.X += speed * DeltaTime; // use X Y or Z.  Use -= to go the opposite way

I also explain it here :slight_smile:

Thanks. This worked well. I also need the actor to be able to move in a curve sometimes. I was thinking I could use VInterpTo() separately in the y direction and the z direction. However, I don’t know how to set the interpspeed to get the two interpolations to finish at the same time. Any advice?

Thanks for the reply. I went with the interpolation option to have more natural movement, but this helped set up the tick function as iwas not previously using it.

Might be easier to use FMath::VInterpTo, then you don’t need to import the UKistmetMathLibrary which is really for blueprints