Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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!

Product Version: UE 4.20
more ▼

asked Jan 09 '19 at 01:31 PM in C++ Programming

avatar image

10 2 3

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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;

     // 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);
more ▼

answered Jan 09 '19 at 04:12 PM

avatar image

1.9k 34 16 51

avatar image Abanoub-G Jan 09 '19 at 07:23 PM

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.


avatar image the_batch Jan 09 '19 at 09:39 PM

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!

avatar image Abanoub-G Jan 11 '19 at 10:57 AM

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);

avatar image Abanoub-G Jan 11 '19 at 11:25 AM

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 (https://api.unrealengine.com/INT/API/Runtime/Core/Misc/FApp/DeltaTime/index.html) doesn't really say much about it.

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

avatar image BenVlodgi Jan 12 '19 at 04:31 AM

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.

avatar image Abanoub-G 6 days ago

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

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question