Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
See one possible solution to this practice task.
Provide feedback
We're looking for feedback on this new type of practice workshop. When you're finished, could you please review the workshop by filling out this short form?

0:00
So, how did you do?

0:01
If you tried a number of different solutions, you probably saw the sphere

0:05
move in all sorts of strange ways, or completely disappear off the screen.

0:11
Those are the types of problems you'll see happen when making games.

0:14
And often,

0:15
seeing how things react to your code can be helpful in finding the right solution.

0:20
Now, let me show you my solution.

0:23
In the hierarchy, I'm going to select the sphere GameObject,

0:29
and then in the Inspector I'm going to choose Add Component.

0:35
And I'll type, OscillateMovement.

0:43
Create the new script and then open it for editing.

0:49
First, I'm going to delete the Start method, as I will not need it.

0:56
Then, inside of the Update method I want to

1:01
change the position of this GameObject every frame.

1:06
So I'll type transform.position and

1:12
then an = for the assignment operator.

1:17
Alternatively, we could also adjust the local position.

1:21
But I'm just going to use the global position values

1:25
to keep things a bit more focused.

1:28
Now, what should we set this to?

1:30
Well, the position variable on the transform class is of type Vector3.

1:37
So the other side of this assignment operator also needs to be a Vector3.

1:43
So I'll create a new Vector3 and then I'll open and

1:49
close the parenthesis and end the line with a semi colon.

1:56
Great, now a Vector3 has an x, y and z position.

2:01
We want to move the sphere from left to right.

2:05
Which from the perspective of the game camera is along the xaxis.

2:10
So that means y and z should stay the same.

2:15
We can keep them at the same values by using the existing

2:19
transform position values, like this.

2:22
So for x we'll just set it to 0f for now, we'll come back to that in a moment.

2:30
For y we'll set it to transform.position.y,

2:36
and then for z we'll set it to transform.position.z.

2:42
Now lets see if we can try to figure out what this x value should actually be.

2:48
This is where I'm going to use the Sin method.

2:52
I'm going to type this out, and then I'll explain it.

2:56
So I'm going to say Mathf for math functions, then .Sin.

3:01
So the Sin method.

3:05
I'm going to pass Time.time.

3:07
And then I'll multiply it by 2f or 2 float.

3:17
The Sin method takes a float value in radians and

3:22
returns a number between negative one and positive one.

3:25
We could get a bit more fancy and calculate theta using a time period and

3:30
an amplitude for our sine wave, but using Time.time without

3:36
any scalar value will just leave the amplitude at 1.

3:41
So this number will gently move between negative 1 and positive 1.

3:48
And then back again.

3:50
Then, by multiplying that value by another positive float, in this case,

3:55
2, we can increase that value to negative 2 and positive 2.

4:02
I'm going to save the script and return to Unity.

4:07
And now if we hit the Play button,

4:12
You can see that the sphere moves from left to right along the xaxis.

4:19
And if you look at the transform position values for

4:23
the xaxis, you can see that it almost gets to negative 2,

4:28
then it returns, it almost gets to positive 2, and then back again.

4:36
There are many different ways to solve this problem, and

4:39
perhaps you came up with a similar solution that's different from mine.

4:44
And that's okay.

4:45
And if you weren't able to come up with a solution, that's okay too.

4:49
Try going back and

4:50
doing it again without looking at my solution to get some additional practice.

4:56
Have fun in Unity and I'll see you again soon.
You need to sign up for Treehouse in order to download course files.
Sign up