Building Flappy Bird #5 – Movement
Time to Move
So now that we have all of our seaweed, it’s time to start moving through them.
If you remember from before, we’re not actually going to move our fish.
Instead, we’ll move the seaweed past our fish.
Let’s create a new script named “MoveLeft”
Here’s our starting MoveLeft file.
using UnityEngine; using System.Collections; public class MoveLeft : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }
For our “MoveLeft” script, we don’t need anything in the Start method, so delete that now.
In our Update method, we want the object the script is on to move to the left, so add the following line of code.
transform.Translate(Vector3.left * Time.deltaTime);
The transform here is the transform you see in the inspector with the position, rotation, and scale.
Translate just moves the transform’s position in the direction and magnitude of the Vector3 we pass in.
What we’re passing in is Vector3.left multiplied by the amount of time that has passed since the last frame.
If you remember, our game will run somewhere between 30 and 90 frames per second. Because the frame rate is variable, we want to use the amount of time passed since the last update. This makes it so our seaweed move the same speed regardless how fast our device can run the game.
The final script should look like this
using UnityEngine; using System.Collections; public class MoveLeft : MonoBehaviour { // Update is called once per frame void Update () { transform.Translate(Vector3.left * Time.deltaTime); } }
….
Time.deltaTime is the amount of time passed since the call to Update(). This number is generally really small around 0.0166. It’s calculated in Unity3D by dividing 1 by your framerate. (1/60 = 0.0166)
Now go back to the Editor and select the “seaweed parent” in our Project View.
With the “seaweed parent” Prefab selected, look to the Inspector. (the one in the Project view is the Prefab)
Add the “MoveLeft” script to our “seaweed parent”.
Now try playing again.
If your seaweed isn’t moving, go to your code editor and make sure you saved your changes to the “MoveLeft” script.
If all went well, you should see your seaweed all moving to the left and your fish appears to be swimming forward.
Why Prefabs are Great
Once you’re done playing, I want you to select one of the seaweed’s in your Hierarchy (it doesn’t matter which one)
Notice that the “MoveLeft” script was added to it.
This is where the power of Prefabs comes into play.
Any change you make to the Prefab will be automatically applied to placed instances of that Prefab. (this applies in all of your scenes when you have multiple)
If you take a closer look at the Transform, you’ll notice the Position & Rotation are bold.
Properties that are bold are not using the values from the Prefab. If you modify a property of a GameObject in the Hierarcy View, it will become bold and no-longer take changes from it’s Prefab.
Speed things up
Right now, our seaweed is moving pretty slow. Let’s modify the “MoveLeft” script to make the seaweed speed adjustable.
Edit your “MoveLeft” script to match this
using UnityEngine; using System.Collections; public class MoveLeft : MonoBehaviour { [SerializeField] private float _speed = 5f; // Update is called once per frame void Update () { transform.Translate(Vector3.left * Time.deltaTime * _speed); } }
Here you can see we’ve introduced a variable with the [SerializeField] attribute on it just like we did previously with the fish.
We then multiply our translation by that new “_speed” variable.
We set the default for _speed to 5f, so it should move 5 times faster than before.
Try playing again.
For me, 5 seems a bit too fast. Because we used [SerlaizeField], we can adjust this speed directly in the editor.
Select the Prefab for our seaweed and adjust the speed until you find a # that feels right.
The final speed I used is 2.5
If you accidentally did your editing on an instance of the seaweed from the Hierarchy instead of the Prefab in the Project view, no problem!
You can actually apply your changes from a placed instance to it’s Prefab (and all other instances) by simply clicking the “Apply” button at the top of the inspector.
Let’s play some more
Give the game another try and see if you can get through all the seaweed.
They’re Gone!
If you’re any good at this game, you noticed all the seaweed disappeared to the left.
You may be wondering if you should add more seaweed to make the level bigger, or if you should move the fish, or maybe the seaweed?
If you weren’t, start wondering now and see what ideas you come up with.
…
…
While there are many different ways you could accomplish making this game go on longer, the easiest and best is to just move the seaweed once it goes out of view.
To do this, we need to go back to our “MoveLeft” script.
Change your script to match this
using UnityEngine; using System.Collections; public class MoveLeft : MonoBehaviour { [SerializeField] private float _speed = 5f; // Update is called once per frame void Update () { transform.Translate(Vector3.left * Time.deltaTime * _speed); if (transform.position.x < -15) { transform.position = new Vector3(15, 0, 0); } } }
Let’s focus on the new lines of code. Lines 13-16
First, we look at the X value of the transforms position.
If that X value is less than -15, we execute the code inside the brackets { }
The code in the brackets is setting the position of the seaweeds transform.
The value we’re setting it to is 15 for the X and 0 for Y & Z. [15, 0, 0]
So all we’re really doing here is checking if a seaweed moved far enough to the left. (negative X values are left of our fish who’s at 0)
If the seaweed is far enough over at -15 or further, we move it back to the right, but far enough off the right side that our players won’t see it on their screen.
Save the script and play again.
I’ve split my Scene & Game views again here. I recommend you do the same to get a good idea of what’s going on.
Next – Randomization & Ground – We’ll add some randomization, some ground, and a couple props.