Unity Project – A Moving Track
Now, we learned to make a moving track, as a part of creating a 2D Simple Racing Game. How to make it:
- Create 3 folders in your asset lists : Scripts, Sprites and Scenes.
- Drag a car and a track to your Sprite Folders.
- Drag your car to Scene View and immediately your car is a part of our new small game
- Now we need to put our track to the scene as well. But first, click on the Track, and on inspector change Sprites into Texture. Therefore, still in inspector, change wrap mode into repeat. Finally, apply
- On the screen, click on Game Object. Create a 3D Object named “Quad”. Put it on a Scene, and adjust it as if it were the track. Use Q-W-E-R
- Make a Directional Light there, adjust it your way then drag your track onto it. MAKE SURE that your track is a part of the quad you created before. Otherwise it will mess up with your script later on It pretty much looks cool for now
- Now the interesting part begins. We want to make the track move, so it will look as if the car moved. Create a C# script on your folder and let we begin
- Declare a public float variable so we can access it on inspector later on. Therefore, also make a Vector2 variable.
- On the update method, we want to change the vector of Quad (which in this case is the track) in Y coordinate. We don’t want the track to move horizontally, but we want it to move vertically. How much it changes depends on the variable velocity inputted later on. We use Basic Physics knowledge in order to think this out
- Now ultimately, we want to input that VECX variable into a texture offset. Enter this under that code
GetComponent<Renderer> ().material.mainTextureOffset = vecx;
- On your quad track inspector, add your script to it. Set the speed the way you want, press play and BOOM, THE CAR IS MOVING. Although its actually the track that is moving
And there it is, my first time using Unity 5. I really am happy that I managed to do this simple thing. I’ll probably cover more Unity tutorials in my Article as I learn more and more. Thank you very much
Author:
- Yogi Udjaja
- Houwen Lie