Creating a Level - Unity 3D #1

I've had some dealings with Unity before, but I'm still a stranger to it. But now I have to put a stop to it. I will make a 3d game with Unity. Of course, it would be more accurate to say prototype rather than game. In this process, I will write down the topics that I do not know on this blog. 

In this post, I will create a scene. This scene will contain some cubes and one sphere. This sphere is controlled by player. 

The default screne already came as default with new project. Firstly, I renamed the default scene as Level1. We can do this in Assets/Scenes folder. Also, if we want to add new levels, it will be enough to create a new scene and add it to this folder.

The first thing I need are cubes. I will use them as intermediaries. After all, there will be gravitational force in the game, but this force will only be valid for the sphere. So the cubes will not be affected. They will simply be a platform for the sphere.

But before that, let's create two folders in our project. These folders are Scripts and Prefabs.

  • Scripts will contain source code as C#
  • Prefabs will contain reusable objects like cubes.
Unity Project Directory

Right click on top of the hierarchy panel and click 3D Object >  Cube and 3D Object > Sphere. 

Cube and Sphere

Drag both objects into the prefabs folder. In this way, we will not need to make changes one by one every time we create an object. Of course, you have to make the necessary changes before you put them in the prefabs folder. 

But we need to create Materials folder also. I create two materials called Cube and Sphere in this folder. I gave blue color to the sphere, and the color of cube material is brown. Then drag these objects into the prefabs folders.

Let's start building the level.

Building Level on Unity

Now, I want to give gravity force for Sphere. There is a component to make it that is provided by Unity.

Click on the sphere and look at the inspector, and click Add Component and choose RigidBody component, after that run the project.

Our red ball collided with the block:
It's time to control the sphere, I mean "the player". So let's create a script called Player in Scripts folder. We will type C# code in this section. We need to manipulate transform values. Because transform component contains position settings, and we will change this values via our keyboard at the game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
    public float speed;
    public float jumpSpeed;

    void Start() 
        speed = 5f;
        jumpSpeed = 10f;

    // Update is called once per frame
    void Update()
        if (Input.GetKey(KeyCode.W))
            transform.position += new Vector3(-1, 0, 0) * speed * Time.deltaTime;
        if (Input.GetKey(KeyCode.S))
            transform.position += new Vector3(1, 0, 0) * speed * Time.deltaTime;
        if (Input.GetKey(KeyCode.D))
            transform.position += new Vector3(0, 0, 1) * speed * Time.deltaTime;
        if (Input.GetKey(KeyCode.A))
            transform.position += new Vector3(0, 0, -1) * speed * Time.deltaTime;

            transform.position += new Vector3(0, 1, 0) * jumpSpeed * Time.deltaTime;
It's not very good code, but it's not very important for a start. I also added jump movement.
Unity Object Movement
I want to follow the ball with the camera. Currently, we don't need to type a script for the camera to follow the player. We will just add this Main Camera to the player like the below:
Third Person Camera
In addition, I made 4.0f of the Y value from the position of the transform:
Camera Position in Unity
The result is like this:
Third Person Camera Movement

I want to add one key game object to the last block. When the player collides with this object, this level must end and move on to the next level. I created a game object, and also I added RigidBody component to it:
Game Object in Unity
It was created in the same way we created other game objects. 

We need to create the second level. This time we position the blocks on the stage diagonally:

Creating Another Level on Unity

We will save as Level2 this scene. We have two levels right now:
Different Levels Unity

Choose Level1, and open File > Build Settings, click Add Open Scenes. Do it same operation for Level2.
Build Settings

Let's create a new script for key. After creating the script, drag it to the key object in Level1.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Key : MonoBehaviour
    // Start is called before the first frame update
    void Start()

    void OnTriggerEnter(Collider collided) 
        if(collided.gameObject.tag == "Player")

    void Update()
But this code will not work. Firstly we need give a tag to Player:
Tag Unity

Also we need to change the collider of key like this:
We must uncheck Gravity and Kinematic in RigidBody of key:
Let's test it:

That's cool, but there is a problem about the view of Level2, it's dark. I choose the Level2 and then go Window > Rendering > Lighting and then click New Lighting Settings, after that uncheck Auto Generate and click Generate Lighting. The problem should be solved, and I don't know why it happens, probably it's a bug of Unity.
Solved Dark Scene Issue Unity
That's enough for now. 

No comments:

Post a Comment