Moving box using C++

Hi,
I am not very familiar with using C++ on unreal engine. I have managed to create a static mesh using C++ now I want to make it move back and forth using C++. I am pretty sure it is simple task but i am just not experienced enough. Most of the help I found on the internet uses blueprints. A detailed guide with the associated commands would be much appreciated.

Thanks in advance!

Store an offset alpha, update that on tick and use it to set the position. Calculate the position from a lerp between start and end points.

I’ve written an example in C++, though I haven’t tested any of it, so you may need to fiddle with it.
I recommend using Blueprints if you are a beginner with Unreal Engine or with programming. You can do much more with BP’s faster as a beginner.

Also most Blueprint tutorials can be replicated in C++. All Blueprint functions link to a C++ function.

UStaticMeshComponent *Mesh;
FVector StartingPosition;
FVector LocalOffsetDirection = FVector(0,0,1000);
FVector EndingPosition;
float MeshOffsetAlpha = 0;
bool HeadingIn = false;
float InterpTime = 10;

AMyActor::BeginPlay
{
	// Setup Mesh attachment and stuff
	
	StartingPosition = Mesh->RelativeLocation;
	EndingPosition = StartingPosition + LocalOffsetDirection;
}


AMyActor::Tick(float DeltaTime)
{
	// Calculate the new lerp alpha
	MeshOffsetAlpha += (DeltaTime / InterpTime) * (HeadingIn ? -1 : 1);

	// Decide if the Mesh needs to flip arround
	if (!HeadingIn && MeshOffsetAlpha > 1)
	{
		HeadingIn = true;
	}
	else if (HeadingIn && MeshOffsetAlpha < 0)
	{
		HeadingIn = false;
	}

	// clamp the alpha so we don't go past our points
	MeshOffsetAlpha = FMath::Clamp(MeshOffsetAlpha, (float)0, (float)1);

	// Update the Mesh's actual location
	FVector NewPosition = FMath::Lerp(StartingPosition, EndingPosition, MeshOffsetAlpha);
	Mesh->SetRelativeLocation(NewPosition);
}

Thanks a lot for your reply. I have been using unreal engine for a while now and I am quite good at using blueprints now. However, now I need to use C++.

When I compile your code I get this error:

error: no member named ‘GetRelativeLocation’ in ‘UStaticMeshComponent’
StartingPosition = MovingBoxMesh->GetRelativeLocation();
~~~~~~~~~~~~~ ^

Any idea what am I missing. Note that instead of “Mesh” mine is called “MovingBoxMesh” and I have made sure to replace all of the “Mesh” in your code with the name I have… Please advice on what I am missing.

Thanks

Thanks a lot for your reply. I have been using unreal engine for a while now and I am quite good at using blueprints now. However, now I need to use C++.

When I compile your code I get this error:

error: no member named 'GetRelativeLocation' in 'UStaticMeshComponent'
     StartingPosition = MovingBoxMesh->GetRelativeLocation();
                        ~~~~~~~~~~~~~  ^

Any idea what am I missing. NOTE that instead of “Mesh” mine is called “MovingBoxMesh” and I have made sure to replace all of the “Mesh” in your code with the name I have… Please advice me on what I am missing.

Thanks

Try just MovingBoxMesh->RelativeLocation

USceneComponent uses the SetRelativeLocation(…) function as a setter for the component’s location, but it seems there is no equivalent getter function.

So just access the location vector directly!

Hi, that did sort out the error. However, I still didn’t get it to work. I have been trying to play with it, but I don’t really under stand what is DeltaTime that is being used in this line below? MeshOffsetAlpha += (DeltaTime / InterpTime) * (HeadingIn ? -1 : 1);

Right cheers, got it to work now. The problem was with this line MeshOffsetAlpha = FMath::Clamp(MeshOffsetAlpha, (float)0, (float)0);. For some reason it doesn’t make it work. Once I have commented it out, it started working fine. Would appreciate it if you tell why is this line stopping it from working.

Also, it would be great if you tell me what DeltaTime is. This link (DeltaTime | Unreal Engine Documentation) doesn’t really say much about it.

Is it the real time? OR is the engine’s physics clock or rendering clock? or what?

Oh, that should have read. MeshOffsetAlpha = FMath::Clamp(MeshOffsetAlpha, (float)0, (float)1);

which prevents the float from being lower than 0, or heigher than 1. It was just a precautionary step to prevent the cube from going beyond the lerp bounds.

DeltaTime is the number of seconds that has past since the last tick. If your game is running at 60 fps, the value of DeltaTime will be 1/60. Multiply movement amounts by this number to ensure that the total movement will be constant over 1 second with varying frames per second.

Also known as DeltaSeconds, here’s a better definition for it AActor::Tick

Game time elapsed during last frame modified by the time dilation

I’ve updated the original answer to fix that typo and getter call.

BenVlodgi, Thanks a lot for all of your answers!!! You have been really helpful. Really appreciate it.