Programming Guide Begginer question

Hello guys,

I’m totally new to UE4 and just started at the programming section. So in the first guide there’s the following code:

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
	RunningTime += DeltaTime;
	SetActorLocation(NewLocation);
};

Now what I can’t really understand is DeltaHeight calculation. What is that value for and how can it affect the actor’s position?.
I understand everything else like what a Tick is, etc., just can’t get how does the time calculation from line 2 affect the actor position to be used in the line under it.

Also, in which moment it decides to move up and to move down? When and how does that change occur?

Thanks.

I suggest you to read this: Unreal Engine Beginner FMath::Sin - Programming & Scripting - Unreal Engine Forums

Okay thanks, some questions (because I really suck at maths)… Now how does it decide when to go up or down? I assume it goes down when Delta Height is negative and up when it’s positive. But I tried changing the scale from 20 to 100 and it moves faster but also makes a longer distance until it changes to up or down. Why is that and how could it be changed?

already did, but that doesn’t answer my question of what DeltaHeight affects the position of the actor and how it decides to make the actor go up and down

Ok, I’ll try to explain that, Sin is a trigonometric function which goes from -1 to 1, depending on the value of the angle. 0 degrees corresponds to 0, while 90 degrees corresponds to 1, from 180 to 360 sin is negative and goes from 0 at 180 degrees to 1 at 270 degrees. The input values are not degrees, but differences of time. DeltaTime as you can see on docs is the time elapsed during last frame modified by the time dilation, while RunningTime is a counter which increments since the game started. So DeltaHeight can go from -1 to 1 because it’s a difference between two Sin functions. When we calculated the DeltaHeight, we can increment the Z location of the actor, but since DeltaHeight goes from -1 to 1, we need to scale it to have our desired moving rate. Multiplying it by 20 sets our scale to 20 units, and multiplying the 20 scale to a negative number decreases the value of the new Z location instead of increase that.

Ok, Sin is a periodic function, so when the angle is 360, the function is repeating. For example the Sin of 540 is the Sin of 180. So the up and down function is periodic like the Sin function. If you want to adjust the speed of the movement and the amount of movement, you should use a different system, for example I would use the FInterpTo function (FMath::FInterpTo | Unreal Engine Documentation).

Okay thank you, it’s now clear :slight_smile:

Hi, I hope u still here. What do u mean “from 180 to 360 sin is negative and goes from 0 at 180 degrees to 1 at 270 degrees”? Shouldn’t it be -1 at 270 degrees or it’s just a typo? Also I cannot understand this “So DeltaHeight can go from -1 to 1 because it’s a difference between two Sin functions”. I understand the sin is a trigonometric function which goes from -1 to 1, so the value of both FMath::Sin(RunningTime + DeltaTime) and also FMath::Sin(RunningTime) will within the range of -1 to 1 right? Then supposing that Sin(RunningTime + DeltaTime) is 1 and Sin(RunningTime) is -1, then we will get a 1-(-1) = 2 ?