# How to Move the object "Natural" by C++?

**URL:** <https://forums.unrealengine.com/t/how-to-move-the-object-natural-by-c/397203>\
**Category:** Character & Animation\
**Tags:** unreal-engine, acceleration, question, CPP\
**Created:** [July 10, 2017, 1:55pm UTC](https://forums.unrealengine.com/t/how-to-move-the-object-natural-by-c/397203 "2017-07-10T13:55:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rexion\_ize](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Rexion\_ize](https://forums.unrealengine.com/u/Rexion_ize)\
**Post date:** [July 10, 2017, 1:55pm UTC](https://forums.unrealengine.com/t/how-to-move-the-object-natural-by-c/397203/1 "2017-07-10T13:55:29Z")

</div>

I want to move some object to move “Natural”.

The Acceleration graph of the “Natural Move” is like this:

![191849-acceleration.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/d/4/fd4b2bb9d3d97583029e88c114b0d2524d4b1a5e.png)

(x: Time, y: Acceleration)

Than, the graph of velocity and displacement would be following graph:

 ![191851-캡처.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/a/a/1aa2c48725f04a81114975bfba37c03d493b92a4.png)

I implemented this movement using SetActorLocaion() function like this:

```
// Move the object by y-axis to _MoveWidth
void ASomeObject::MoveSomeObject(float _MoveWidth, float _MoveTime)
{
    // Ignore if the object is already moving
    if (bIsMoving)
        return;
    ElapsedTime = 0.0f;
    MoveWidth = _MoveWidth;
    MoveTime = _MoveTime;
    // The displacement of the object can be expressed:
    // (MoveWidth)/(MoveTime)^3 * (3*(MoveTime) - 2*(ElapsedTime)) * (ElapsedTime)^2
    MagicNumber = MoveWidth / (MoveTime * MoveTime * MoveTime);
    // Flag for checking the object is moving
    bIsMoving = true;
    // Store the current location and target location
    DefaultLocation = this->GetActorLocation();
    TargetLocation = DefaultLocation + FVector(0, MoveWidth, 0);
    PrevX = DefaultLocation.X;
    PrevY = DefaultLocation.Y;
    PrevZ = DefaultLocation.Z;
}

void ASomeObject::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    if (bIsMoving)
    {
        ElapsedTime += DeltaTime;
        FVector PrevLocation = this->GetActorLocation();
        const FVector NewLocation = FVector(PrevX, PrevY + MagicNumber * (3 * MoveTime - 2 * ElapsedTime) * ElapsedTime * ElapsedTime, PrevZ);
        SetActorLocation(NewLocation, false, nullptr, ETeleportType::TeleportPhysics);
        // If the velocity becomes negative
        if ((NewLocation.Y - PrevLocation.Y) / DeltaTime <= 0)
        {
            bIsMoving = false;
            // Revise the final location to Target Location
            SetActorLocation(TargetLocation, false, nullptr, ETeleportType::TeleportPhysics);
            ElapsedTime = 0.0f;
        }
    }
}

```

This code works pretty well, but It’s just a trickery. The object doesn’t have velocity but it just teleported by computed location.

So I want to make the object using movement vector. How to implement it??

---

<div class="post-metadata">

**Author:** ![42EspoiR](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@42EspoiR](https://forums.unrealengine.com/u/42EspoiR)\
**Post date:** [July 10, 2017, 2:40pm UTC](https://forums.unrealengine.com/t/how-to-move-the-object-natural-by-c/397203/2 "2017-07-10T14:40:51Z")

</div>

What do you mean by “movement vector” ?

You can simplify your code using Lerp Vector with a custom ue4 curve that give the alpha.

---

<div class="post-metadata">

**Author:** ![Rexion\_ize](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Rexion\_ize](https://forums.unrealengine.com/u/Rexion_ize)\
**Post date:** [July 11, 2017, 12:49am UTC](https://forums.unrealengine.com/t/how-to-move-the-object-natural-by-c/397203/3 "2017-07-11T00:49:31Z")

</div>

Oh, sorry for my poor English. I meant that I want to give a real velocity to the object, by using like MovementComponent::AddInputVector().  
Also, using Lerp sounds good for me, so I’ll try it. Thank you.

---

<div class="post-metadata">

**Author:** ![42EspoiR](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@42EspoiR](https://forums.unrealengine.com/u/42EspoiR)\
**Post date:** [July 11, 2017, 9:21am UTC](https://forums.unrealengine.com/t/how-to-move-the-object-natural-by-c/397203/4 "2017-07-11T09:21:01Z")

</div>

Why not add a movement component in your Actor header ? It should work in cpp

in .h

```
UProjectileMovementComponent* MoveComp;

```

in cpp,

```
AYourActor::AYourActor(const class FObjectInitializer& PCIP)
{
    ProjectileMovement = PCIP.CreateDefaultSubobject<UProjectileMovementComponent>(this, TEXT("ProjectileMovement0"));
}

```

see [this post](https://answers.unrealengine.com/questions/170269/spawn-blueprints-with-c-code.html)

---

<div class="post-metadata">

**Author:** ![SvarogZ](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SvarogZ](https://forums.unrealengine.com/u/SvarogZ)\
**Post date:** [July 11, 2017, 10:34am UTC](https://forums.unrealengine.com/t/how-to-move-the-object-natural-by-c/397203/5 "2017-07-11T10:34:58Z")

</div>

Next link can be helpful

[UMovementComponent](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/UMovementComponent/)

---

<div class="post-metadata">

**Author:** ![MatzeOGH](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MatzeOGH](https://forums.unrealengine.com/u/MatzeOGH)\
**Post date:** [July 11, 2017, 10:46am UTC](https://forums.unrealengine.com/t/how-to-move-the-object-natural-by-c/397203/6 "2017-07-11T10:46:06Z")

</div>

Take a look at the floating pawn movement component.  
Use SafeMoveUpdatedComponent to move your component through the world, this handels hitting other level geometry for you. At the end of your update call UpdateComponentVelocity(). This ensures that your actor has the correct velocity if you need it afterwards
