0

I'm new to Unreal Engine and I'm trying to move an actor from his original placement location to point B and then back to the original placement and then to B in a loop. How do I do this through C++? I don't know much about all of this but I have a homework on researching how it works. I know how to make the C++ Actor class but in the C++ class how do I assign it to a static mesh and how do I make it do all the movement with the set actor location?

Thank you

QZAK
  • 63
  • 1
  • 6
  • Another solution is to add navmesh and AI which will go to the point over the mesh. There is [example on using it with blueprint](https://www.youtube.com/watch?v=z7tXpqSUzQM&ab_channel=NebulaGamesInc), but there is somewhere surely one using only Cpp code. – Tatranskymedved Jan 28 '21 at 14:39

2 Answers2

1

You will want to Override the Tick function of your Actor, this then enables you to run code each frame, with this you can then incrementally make small adjustments to the Actors location towards the TargetLocation.

This can be achieved via an Interpolation function.

FMath::VInterpConstantTo(const FVector& Current, const FVector& Target, float DeltaTime, float InterpSpeed);

Using this function you can get a delta FVector between the CurrentLocation and TargetLocation over time, which would then become your Actors new location.

Once it reaches its TargetLocation, you can then swap the TargetLocation value of this function to be the original location of the Actor, thus creating a ping pong action in its movement.

DevilsD
  • 545
  • 1
  • 5
  • 12
0

Okay so I solved it by doing the following:

I added the following to the .h file

    APlateUp();

    UPROPERTY(EditAnywhere)
        UShapeComponent* Root;

    UPROPERTY(EditAnywhere)
        UStaticMeshComponent* MyMesh;

float RunningTime;

in the class public

And then in the .cpp file I added

    Root = CreateDefaultSubobject<UBoxComponent>(TEXT("Root"));
    RootComponent = Root;

    MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh"));
    MyMesh->AttachTo(RootComponent);

in the main function and then I added

    FVector NewLocation = GetActorLocation();
    float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));

    NewLocation.Z += DeltaHeight * 200.0f;
    RunningTime += DeltaTime;
    SetActorLocation(NewLocation);

Of course you can change the NewLocation.Z to X or Y and you can change the force of which the object moves with.

These solutions were provided by watching Reuben Ward on YouTube so more explanation on all of this there. I'm just answering my question so if anyone ever has the same issue they can get the answer easily. Also, if you want to stop the object at a certain location you can do:

    if (NewLocation.Z < 7700.0) {
        float DeltaHeight = ((RunningTime + DeltaTime) - (RunningTime));
        NewLocation.Z += DeltaHeight * 100.0f;
        RunningTime += DeltaTime;
        SetActorLocation(NewLocation);
    }

And of course change the NewLocation.Z to X or Y and the 7700.0 to whatever location you want on that axis. This checks if the current new location is less than the value you provide and if so it keeps moving the object on the axis. This is out of my own so if you have a question about this function specifically, feel free.

QZAK
  • 63
  • 1
  • 6