Simple Platformer with Unity — Part 1

Hello friends, I started to learn unity. I am watching zenva video course which you can get super cheap Humble Bundle. And I want to share with you what and how I build. Currently, I am building a simple 3d platformer. My code can be found on GitHub.

In the current game, I am going to create few objects such as ground, coins, enemies, goal, and platforms. In the current prototype, I am using only default unity components. Let’s start with the material creating. Materials are used in conjunction with Mesh Renderers, Particle Systems and other rendering components used in Unity. They play an essential part in defining how your object is displayed. To better organize unity project create a materials folder and create there 6 materials object.

To create folder press right mouse button on assets, choose Create and then Folder:

To create material open Materials folder, press right mouse button, choose Create and them Material:

Modify material configuration as you like. My example of coin material configuration:

Repeat this six time. You should have something similar:

Now we are ready to create our enemy, coins, enemy, platforms, ground, and goal. Objects can be created in a similar way as we did material. Press mouse right button, then 3d objects and choose an object which you want to create.

Also, similar objects can be grouped by creating Empty Object and moving the rest of the objects in Empty one. As you can see from the picture above I have three Empty Objects — Enemies, Coins, and Environment. And Environment contains two platforms (cube object) one ground(plane object). Coins contain one coin(cylinder object). Enemies contain one enemy(cube object). Also, there is ungrouped objects goal(sphere) and player(cube). Locate this objects on the screen as you wish. And assign material to each object by moving a material icon on the object itself.

In order to make coin look similar to a coin cylinder object need to be changed. Open coin objection by pressing mouse left button on it. And change Scale parameters.

Later we are going to add more enemies and coins, and it’s can be hard to modify the parameters of each object. We can prevent this by creating prefabs object from existing. As usually create prefabs folder and move coin and enemy object into this folder. And each object you create from this prefab will have the same parameters. Also if you need to override any parameters this can be done by choosing a newly created object and change it parameters. Rest of the similar object will have a prefab parameters.

As finals step in the current post, I will add a script which makes a coin to rotate.

Create Scripts folder in the same way as we created Materials. And create c# script in this folder with name CoinControler. Right click on this script will open it. Visual Studio will be opened.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinController : MonoBehaviour { public float rotationSpeed = 100; // Update is called once per frame
void Update () {
float angleRotation = rotationSpeed * Time.deltaTime;
//rotate coin
transform.Rotate(Vector3.up * angleRotation, Space.World);

Imports and CoinController will be created automatically by Unity. Also, there will be two methods created Start and Update.

I removed Start method because I am not going to use it right now. rotationSpeed variable

public float rotationSpeed = 100;

Rest of the code will be added in Update method. At first, one more variable will be added.

float angleRotation = rotationSpeed * Time.deltaTime;

We can not use just rotationSpeed because this will make a coin to rotate very slow on a slow computer and very fast on powerful computers. That’s we need to multiply rotationSpeed on time in seconds it took to complete the last frame.

Time.deltaTime is the time in seconds it took to complete the last frame.

And finally, we can rotate coin using Transform.Rotate. Also, Vector3.up and Space_World is used here.

Vector3.up just means that we are going to change y coordinate and Space_World applies transformation relative to the world coordinate system.

//rotate coin
transform.Rotate(Vector3.up * angleRotation, Space.World);

Save the script, back to Unity drag this script on coin object. And start a game.

And you should see coins is rotating.

Don’t forget to check Humble Bundle to get access to Unity assets and zenva video course just for 15$

Check my blog —