How can I change the period of oscillation of an object?

Hi there, thanks for looking at my question!

I have been learning C++ for a month or so to try get a handle on it for general use but also to have a better understanding when I started learning Unreal.

I am in the first C++ unreal tutorial where toy make a floating Actor that bobs up and down and I understood it, but at the end it asks you to add 2 more axis of movement at different rates.

I thought I had the answer but I see now that I was just increasing the value of Sin by multiplying the delta time by a different amount for each direction, which increases the length the object moves, but it takes the same time in all directions, resulting in a vector motion. What I want is for it to take different amounts of time to complete a full cycle in each axis so that it looks like it is bobbing about in all directions.

// Called every frame

void AFloatingActor::Tick( float DeltaTime )
{
    Super::Tick( DeltaTime );

    FVector NewLocation = GetActorLocation();
    float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    NewLocation.Z += DeltaHeight * 20.0f;
    float DeltaDepth = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    NewLocation.X += DeltaHeight * 10.0f;
    float DeltaWidth = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    NewLocation.Y += DeltaHeight * 10.0f;
    RunningTime += DeltaTime;
    SetActorLocation(NewLocation);

So for example I want it to move up and down once fully every 3 seconds, back to front every 2.5 seconds and side to side every 4 seconds.

I hope that is sort of clear. Can anyone help?

It very easy if you think about it, you just scale the flow of time so for example:

(FMath::Sin((RunningTime + DeltaTime)*2) - FMath::Sin(RunningTime*2))

Will move twice as fast :slight_smile:

If you want to accurate cycle per sec you need also multiply frequency by PI*2 (*2 make it full cycle, look link)

(FMath::Sin((RunningTime + DeltaTime)*(TimeScale*(PI*2))) - FMath::Sin(RunningTime*(TimeScale*(PI*2)))

if you want sec per cycle you invert the scale (1/x)

(FMath::Sin((RunningTime + DeltaTime)*((1/TimeScale)*(PI*2))) - FMath::Sin(RunningTime*((1/TimeScale)*(PI*2)))

But note as you use adding to position, change of frequency and aspecially amplitude can cause displacement of the actor, if you want it to be consistent use setting insted of adding, i think changing frequency sould not be a issue if you scale time just at delta time

Thank you so much! I knew it was something simple but my brain wasn’t working. I was putting the modifier in the wrong place.

Thanks!

Sorry to resurrect this old post but I’m confused why the whole sin(RunningTime + DeltaTime) - sin(RunningTime) bit is necessary. If you just want something to oscillate can’t you just do sin(RunningTime)?

You right he could just do RunningTime += DeltaTime; before the computations, he would have similar result, but maybe he wanted 0 value on first tick?