This Lesson Completely Lost Me

Can someone please explain this bit in more detail:

                float cycles = Time.time / period; // grows continually from 0

		const float tau = Mathf.PI * 2f; // about 6.28
    		float rawSinWave = Mathf.Sin(cycles * tau); // goes from -1 to +1

    		movementFactor = rawSinWave / 2f + 0.5f;
    		Vector3 offset = movementFactor * movementVector;
    		transform.position = startingPos + offset;
1 Like

Okay, I know your post is around 5 days old but thought I would post an answer as I have just reached this far in the course.

So let me see if I can talk you through it. I am going to assume SOME knowledge here, but you can very easily look up the holes in my explanation just by using Google.

float cycles = Time.time / period; // grows continually from 0

Cycles is being defined as Time Elapsed divided by (/) how long it takes for each Sine wave to finish (probably easier to think of this from peak-to-peak) and simultaneously how long it takes for the circle to be drawn.
So for instance if a sine wave is defined as 3 seconds and the time elapsed is 30 seconds - 30/3 = 10 cycles.

const float tau = Mathf.PI * 2f; // about 6.28

Tau is being defined as Pi (The ratio of the area of a circle to its diameter). Now if Radians were actually Diameterians (if this were a thing ^^), we would not have to account for the shortfall but because the radius of a circle is exactly half of the diameter, we need to increase the number by a factor of 2 (or 2f in the example).

When I first learned about the degrees/radians argument, I thought why use radians at all? (a radian is around the equivalent of around 58 degrees iirc). However degrees are only useful from the centre of the circle as when you used to use a compass at school to draw an arc, if you are staying on the circumference of a circle its not much use. Essentially no matter how big the circle is, the circumference will always equal its radius multiplied by around 6.3.

What this means is that as we change our period value, our function is calculating the value of Tau because the relationship is staying the same.

Wow that took a while, I think the rest of the code should be fairly self explanatory, however if yuo want em to elaborate into that, I’d be happy to oblige.

Thanks

2 Likes

@Shaun_Jones thanks for laying this out. Your explanation definitely helped me understand a bit more, but is there a way you can define how to directly relates to in-game?

In game, we’re looking to have a movement factor between 0 and 1 so that if you multiply that times the movement length, we can calculate the location that the obstacle should be at a given point in time. That’s what the Sin function is for. It’s always a number between -1 and 1. Hence, we take the sine wave, divide it by two (now it’s a number between -.5 and .5) and add .5 to it (bringing it to the range of 0 and 1).

Then the offse is calculated by multiplying the movementFactor * the desired movement Vector. and added to our current transform position.

1 Like

Okay this is finely starting to make sense. Thanks again @Brian_Trotter. So if 1 is the full movement length (1*movement length), is the movement length also considered the max length? What I mean is, if I push my d button all the way down (1), will it move my character to the right edge of my screen? Or is movement length a defined length to move per frame, so every update?

In the case of this lesson (moving the blocks between two points), movementLength would be the max distance the object would travel from the starting point…
I must specifiy in the case of this lesson, because in another context (moving your character around with a joystick) you may have it set up differently. This particular lesson is about moving a block back and forth.

1 Like

Makes sense. Thanks @Brian_Trotter!

In response to your post, the Sin wave is used in this instance to smooth the movement of the block to make it look more natural. See how it slows down when it gets to its maximum and minimum value, just before it switches direction and how its going its fastest during the middle section of its movement?

If the value simply went from your minimum to your maximum in an instant it would look really odd lol.

1 Like

Privacy & Terms