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);
}
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.
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:
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).
// 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.
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.
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!