Help with programming QuickStart 5

Hello, I tried searching for this answer on the hub and didn’t have any luck, was wondering if any of you guys would be able to help me out. I’m new here and was following the programming quick start guide and got to the part which asks you to:

Add periodic motion on the X and/or Y axis, and multiply the DeltaTime value by a number between 0.6 and 1.4, so your FloatingActor appears to float around freely. This can look great for powerups!

Was wondering if anyone could tell me how that would like in code.?

Currently I have this in my tick function

void AFloatingActor::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
	FVector NewLocation = GetActorLocation();
	float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));

	//NewLocation.X += DeltaHeight * MovementConstant;
	NewLocation.Y += DeltaHeight * MovementConstant;
	NewLocation.Z += DeltaHeight * MovementConstant;
	RunningTime += DeltaTime;
	SetActorLocation(NewLocation);
}

How could I make the floating actor seem a little more randomly floating in the air, rather than floating from one end of a diagonal to another? Was playing around with the rand() function, wasn’t sure if that was the right one to use. Any help is appreciated, thanks.

Hello,

Please note that you can use FMath::RandRange function to get a random number in custom range:

static int32 RandRange
(
    int32 Min,
    int32 Max
)

However, it is also possible to move the actor instantly to the specified location with AActor::SetActorLocation function:

bool SetActorLocation
(
    const FVector & NewLocation,
    bool bSweep,
    FHitResult * OutSweepHitResult
)

For more complex movement logic you can use AI Controlled Pawn.

In Unreal Engine 4, AI functionality is implemented with the help of Behavior Tree object. In particular, it has Move To function:

54443-moveto.png

With the help of Get Random Reachable Point in Radius node, you can get the TartgetPoint for Move To function:

54444-getrandomreachablepoint.png

If you like to learn more about Behavior Trees in Unreal Engine 4, please go here:

Hope this helped!

Good luck!

So, what did you use in the end and How did you use it? I would like to know too, because I’m pretty new :wink:

What if I don’t want to use FMath::RandRange? I want to multiply DeltaTime just like it’s saying in the QuickStart tutorial (5 step), but I already tried everything. The question is: where must I multiply the DeltaTime?

Hey, I would suggest a Curve

UPROPERTY(EditInstanceOnly, Category = Tools, DisplayName = "Test Curve")
 UCurveFloat* FloatingMovementCurve;

You can edit this curve in the asset browser and then just access it’s values via

Curve->GetFloatValue(DeltaSeconds)

This gives you or your designers full control over the effect.

Alternatively just make a Timeline in Blueprint and animate the Powerup this way.

Hello,

Basicly You need to know that to make good looking natural hoovering obove the ground is not so simple task and it can be a little to early for You :wink:

using a curve seems to be good idea, but the downvote for it is that it will float always in the same path. Using behaviour tree for something such simple as hoovering powerup is a bit overkill IMHO.

The way it shoul be truly done is to use noise. Noise is a algorithm that gives You numbers that are random but each subsequent is connected with previous and similiar to it so it is good to animate position. Google “Perlin Noise”.

Well, even though noise gives you nice randomness, I think this is overkill as well. It is just a visual effect. Noise is pretty slow to lookup every frame.
You could just add another curve for rotation and/or make a curve that handles several periods with some variations. Nobody will ever see a pattern if Rotation and Translation have different period times.

I just added this:

// Called every frame
void AFloatingActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector NewLocation = GetActorLocation();
float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
float DeltaWidth = (FMath::Cos(RunningTime + DeltaTime) - FMath::Cos(RunningTime));
float DeltaDepth = (FMath::Acos(RunningTime + DeltaTime) - FMath::Acos(RunningTime));

NewLocation.Z += DeltaHeight * Amplitude;       //Scale our height by amplitude value
NewLocation.Y += DeltaWidth * Amplitude;       
NewLocation.X += DeltaDepth * Amplitude;       
RunningTime += DeltaTime;
SetActorLocation(NewLocation);

}