Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Unity Basics!
You have completed Unity Basics!
Preview
Pipes spawning in the same position every time calls for a very boring game. Letβs add some randomization to the gap heights.
Unity 6 Documentation
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Alright, so we want to randomize our pipes
a bit on the Y axis
0:01
to give it a bit more of a challenge,
so let's jump straight into it.
0:04
Let's open up our pipes spawner script.
0:08
We're instantiating our pipes
0:12
prefab here, and currently we're just
putting 0f as the value for the Y axis,
0:14
so we know this is the value
we want to adjust.
0:19
But we don't want to just hardcode
another number
0:21
in there,
we want something random each time.
0:24
Lucky for us, Unity has a built-in class
0:27
for this, so let's make some space above.
0:29
We just type random with a capital
R and a dot.
0:32
The method we want to use is called range.
0:36
If we hover over it, we see it returns
0:41
a random float in between a minimum float
and a maximum float value.
0:43
Since it's difficult to tell from our IDE,
0:49
let's make some serialized fields
to figure this out in Unity.
0:51
I'd to present this to you
0:56
as a mini-challenge,
as we've created a few variables already.
0:57
We're working with floats,
so what we need are two private
1:01
float variables,
and I'd them to be serialized.
1:03
You can name them anything you
but try to have them make sense.
1:07
Also, we don't need to provide any values
to them yet. Ready?
1:11
Pause me and give it a go.
1:14
How'd you do?
No worries if you got stuck.
1:17
So up top, let's create
a serialized field,
1:21
private, float, and name it minYPosition.
1:26
If you're using VS Code,
you can hold Shift and Alt or Shift
1:31
and Option on Mac and hit the down arrow
to duplicate this line.
1:35
Or you can just copy and paste it too.
1:39
Let's change the variable
1:42
name to maxYPosition.
1:43
Since we haven't provided random.range
any values yet,
1:46
we can see it's giving
us a red error line here.
1:50
If there's ever any errors in your code,
Unity will not be able
1:52
to compile the scripts,
1:56
and we won't be able to see our changes
or playtest our game.
1:57
Here, I'll show you real quick.
2:01
You can see here
we're getting this red error
2:04
in the console stating no overload
for method range takes zero arguments.
2:06
Basically meaning
2:11
that there are a few different ways
to use this range method or overloads
2:12
and none of them work without giving it
some values as arguments.
2:16
So let's just go comment this line
out for now
2:21
and we'll get back to it momentarily.
Save this, and let's go back to Unity.
2:23
Okay I'm going to
2:28
select our pipes object
and hit W for the move tool.
2:29
I'll drag these up until the gap is up near
2:33
the top of the camera
but not going past it.
2:35
I think negative 2 looks good.
2:39
So on our pipes manager, I'll add that in
2:42
as our maximum Y value.
2:45
I'll reselect the pipes and move them down
near the ground.
2:51
Negative 7 looks good to me.
2:54
I'll use that as my minimum value.
2:57
Feel free to use any values
you choose here, as long as they're
3:00
within the camera's view.
3:03
If you have different transform values
on your pipe's objects,
3:04
or something
isn't lining up exactly mine, don't fret.
3:07
Just adjust your highest and lowest points
to what works on your screen.
3:11
Let's head back to the script.
3:15
So we could type this random.range method
directly
3:19
into this vector3 here in our y position,
but to keep things readable and clean,
3:22
let's create
a local variable for this up here.
3:27
Let create a float, name it randomYPosition,
3:30
and set it equal to this method.
3:35
Now we need to plug in our new variables
as the two arguments
3:38
minYPosition
3:42
comma maxYPosition
3:45
This will now return a random float
in that range and save it to this
3:48
new variable.
3:52
Now we just need to plug this variable
into our vector3 down
3:54
here.
3:56
Let's save our script and go test it out.
4:00
Awesome, it's working great!
4:11
We're getting some nice randomization
to our obstacles.
4:13
Okay, we can safely delete
this pipes instance from our scene,
4:17
now that we're spawning
these through our code.
4:21
To get one to spawn
right at the beginning, without waiting
4:24
for our timer initially,
let's pop back into our pipe spawner
4:26
script again.
4:30
Here in the Start method,
we can simply make a call
4:34
to our SpawnPipes method to trigger
one right at the beginning.
4:37
Perfect!
4:47
In the next video,
4:48
we'll handle the logic
for when the player hits a pipe and loses.
4:49
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up