Setting up a timer in C++

Hello! I’m practicing C++ (Well, Really just learning it), and I used the basic c++ tutorial epic games gives out with the cone floating up and down, and I’m trying to make it move back and forth. When copying the code for the z axis but applying it to the x or y axis, it simply scales, so i tried changing it from moving due to delta time to running time, but then it just floats off screen, so i thought i’d make a timer so that it would minus the running time after awhile, but I cant figure out how to make a timer.

// Fill out your copyright notice in the Description page of Project Settings.


#include "FloatingActor.h"

// Sets default values
AFloatingActor::AFloatingActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AFloatingActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// 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;       //Scale our height by a factor of 20
	NewLocation.X +=RunningTime * 06.0f;       //Scale our height by a factor of 20



	RunningTime += DeltaTime;
	SetActorLocation(NewLocation);

}

The NewLocation.X += RunningTime * 06.0f; will make object move continuously in single direction, as RunningTime continuesly increasing forever

What kind of motion you want to create?

Some sort of back and forth thing, Like it goes → for a few seconds or meters, and then <-. Thats possible, right?

Okay, So i changed the variable back to that, but i get this effect when using two axis’s at once:

So is it only possible to make it move along one axis at a time?

Yes, that’s why FMath::Sin was used in the example which you follow. By default it cycles between -1 and +1 infinitely (in time). If you want to use it for different axis then you need to use DeltaHeight rather than RunningTime.

Not sure what kind of motion you want to have at the end so I will stick with this part of your question:

I’m trying to make it move back and forth

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

What you have already in your code by following the Programming Quick Start is correct and can be used to change direction of the motion. By default it goes up and down based on the sine function FMath::Sin and RunningTime which you update every tick (this is your timer already).

If you want the object to “move back and forth” then you just have to update this line:

//NewLocation.Z += DeltaHeight * 20.0f;
NewLocation.X += DeltaHeight * 20.0f;

DeltaHeight is just a number which changes over time. Thanks to the FMath::Sin it goes back and forth rather than going in one direction forever (as in your case when you replaced it with RunningTime).

If you want to update two axis at the same time it should still work.

NewLocation.Z += DeltaHeight * 20.0f;
NewLocation.X += DeltaHeight * 20.0f;

Can you please share your current code once again?

// Fill out your copyright notice in the Description page of Project Settings.

#include "FloatingActor.h"

// Sets default values
AFloatingActor::AFloatingActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AFloatingActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// 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;       //Scale our height by a factor of 20
	NewLocation.X += DeltaHeight * 06.0f;       //Scale our height by a factor of 20



	RunningTime += DeltaTime;
	SetActorLocation(NewLocation);

}

This is it currently, changed x to deltaheight rather then running time, but it does what i showed in the video.

This is what i kind of imagined would happen

Oh, that gives some insight into what I’m doing. Thank you :slight_smile:

I just checked your code in my sample project and cone location is being updated correctly, scale is not being changed at all. Are you sure cone in your scene is not below the floor a bit? “Scale” effect may be just an illusion. Try to move your cone a bit higher to make sure it’s above the floor level.

Also the motion of the cone will be different than what you imagine right now. It will keep moving between two different locations, not four.

281800-conemotion.png

Ah, Okay, it works now. Thank you! Glaring mistake on my part, haha. Works now, but still doesnt use a timer. Ah well, I can learn to do that later. Thank you!

No problem, good luck and have fun. :slight_smile: