Build Crossy Road Replica Part-2



 Note that i doesn't own any copyright over crossy road or on any of the materials related to it. So even though i created the assets these doesn't belong to me. If you are publishing this game ,It is your responsibility to get appropriate permission from the corresponding developers.


If you are visiting this page using phone, it is recommended that you view this page in landscape mode to view this page correctly 

I guess you have covered first part , if not read it 

Now if we look in the Hierarchy it looks too crowded , And when we go forward the no of game object will be increasing and will be hard to manage So , For easy managing the object what we do is create an empty object and make it parent of similar objects:




To do that: Create an empty GameObject (Right click on empty space on hierarchy tab > select Create Empty OR Click on the "+" symbol and select Create empty)

Now to make sense Rename it. for that - right click on that object and select rename or you can directly edit in the inspector tab.{ I renamed it to Environment }

Select all the Objects ( using Shift or control button) and drag them to Environment object. Now all those selected objects will be child of the environment object. And you can hide/show them by clicking the right arrow shown on the environment object.



You can do this in any situation similar to this. Note that by doing so your child object will inherit properties of its parent object . ie if you move/ rotate / scale the environment object this will affect all your child objects. And this is helpful in situations where you need to change the position / scale / rotation of multiple objects as a whole.


Now what we need to do is to place our player character (🐔) To do so, Drag the "Chick" file from "3d files " folder to the Hierarchy or to the scene. And try to adjust values of position, rotation , and scale which you feel like is a good value. mine was:






if you feel like snapping is not allowing you to position it correctly you can disable it by clicking grid and snapping icon mentioned in the previous part ( icon looks like a grid and a magnet near to the left of play button ).

Next step is to adjust the camera to view our character . you can adjust it by selecting camera changing the position and rotation manually using inspector tab or using the move / rotate tools .{ make sure to click and drag the game tab behind scene tab to any other tab group (eg project ,console .. group below so that you can watch camera view live while adjusting.)

An alternate and easy way is to navigate the scene view ( if you need help check this : https://docs.unity3d.com/Manual/SceneViewNavigation.html ) to a view that you would like to be viewed by the player in the game . And select the camera in Hierarchy tab , then go to "GameObject " in main menu > select "Align with view " So that your camera automatically takes position , rotation ..  in order to show the part of the scene your were viewing in the scene tab.my camera transform were : 



Now if you have followed my transform , you should be able to see that from the viewing angle our character is not properly seen (some darkness is there) it is due to shadow. And it present because of one component called Directional Light { you can see it in the Hierarchy tab }. this component acts as similar to sun in real world.(for more info :-https://docs.unity3d.com/Manual/Lighting.html)
So if we change the direction of the sun to a rotation such that the light from the it hits the area we are seeing , we would be able to remove the shadow from the area we are seeing.

To do that , similar to camera, select the Directional Light and go to "GameObject "in main menu >Select "Align with View" .
But for that you should be viewing form the same angle you camera is . if you have change the view then you can go back to it by just select the object from which you want to view(Camera) > then select GameObject "in main menu >Select "Align view to selected ".

now it should be looking like this: 




Now our background is a grey colour which is a default Skybox generated by unity . 
If you don't know about skybox , to understand better consider it to be a sphere with infinite radius whose  inside wall is painted with sky , clouds etc . and when we look from inside we feel like real world sky.

To change this grey colour you can change the skybox but since we don't want any realistic sky / background . we don't use this method. instead we use simple alternative.

ie we ask camera not to render skybox instead a simple colour.
for that select camera in Hierarchy tab and in inspector click on clear flag under camera component > and select solid colour.

And you may notice that colour in game has change to a blue colour. To set your own colour click the background below clear flag and select the colour you want . i chose white and it is not compulsory. 



Now we need to move our character

For that we are writing our first script. 

There are many ide 's for unity script editing  :
 for vscode try these links:
    
Also ,it is not compulsory to use an ide instead you can use any text editor also. If you don't want to install an ide , i recommend notepad++ https://notepad-plus-plus.org/
(a small text editor which is suitable for coding)

So create a folder to place all our script.
Inside that folder right click > select create > select C# Script. And type a name (eg : "controller")
 Make Sure you don't Add space in the script name. 
A Script will be create in that folder.

Now double click to open it. if you have already setup ide it will be open by default.
else sometime it may ask you to select an application to open this kind of file > in that select any text editor. or directly open it using a default text editor 

Note that you can find information,example etc of all functions used in unity from : https://docs.unity3d.com/ScriptReference/index.html

when you open a Unity C# Script it will not be empty instead it consist of some lines of code
For making it simple you ignore first lines now and consider from start()

Read comments (statement starting using "//" ) to understand the code


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Controller : MonoBehaviour
{
    
    void Start()
    {
        // you write here what should be done before game start. and it runs only once
} void Update() { // you write here what should be done in every frame and it runs multiple times } }
now think of what we need to do:
We need to move our chick object a particular distance every time user presses a button.

So we need to check for input every frame , if there is input we need to move. So in order to run this code every frame we write it in the Update block.

How do we check for input?

For that unity has a function called  "GetKeyDown" along with this you have to give which keypress have to be checked in our case up arrow key for that we will write " GetKeyDown(KeyCode.UpArrow) "

but we cannot directly use this because it is inside another class called Input, so we write 

So what this function gives is a bool value , which means true(1) or false(0)
it gives true if up arrow is pressed and false value if we don't press 

to check the value of GetKeyDown we use if -statements . what it does is that it will run the block of code below it only if value is true.hence we can move our player only if GetKeyDown is returning true
So our code Becomes


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.UpArrow)) // runs the below block only if the GetKeyDown is true
        {

        }
    }
}

Now we need to move 
For that we use Translate() , this is a function to move our gameobject
Similar to the GetDownKey this is also from a class called transform so we code like: 
transform.Translate()

but to use this function we need to give it a parameter . which the movement . in Unity 3d there is a simple way to show 3 dimensions ie Vector3.this is a type of data which has 3 components which are value of x axis, y axis, z axis. so these are used in many situation like position velocity ,rotation etc ..

 for example we need to say to unity that the position of object is x=1,y=2,z=3 . so we can write like "Vector3(1,2,3)" ( 1st value corresponds to x, 2nd to y, 3rd to z)

In our case we need to move our object in x axis by 1 every time user presses up arrow
so we write 
" transform.Translate(Vector3(1,0,0))"

But here also there is a problem , which is our function (" Translate ") need a variable (variable is like a container which can be used for storing values) instead of which we are directly giving a value, which is an error.

So to over come this add "new" before Vector3(1,0,0) so our unity feels like it is a variable.
So now our code becomes:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Controller : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.UpArrow)) // runs the below block only if the GetKeyDown is true
        {
            transform.Translate(new Vector3(1,0,0));
        }
    }
}


Now lets try running the game!
But before that save the script and return to unity.Now you may need to wait 4-5 seconds to compile the code.
And we need to add script to our chick gameObject in the Hierarchy tab.
To do that drag the script from project tab to chick gameobject in the hierarchy tab.
now if you select the chick game object you will be able to see the script attached to it in the inspector tab.


Now click the play button and check if our player is moving. it should , if not make sure you have done the above step. if you are still not able to move contact me, i will help you fix it.

Now we are going back to the concept of variables...
For most of you, your character may have moved too long than we need or even it may not be in the right direction.To adjust this we need to modify the value in our code. This is the problem if we directly give the values inside our code , we cannot see or control it via unity. so we use variables ,
As i have said these are like container and can hold any values.

this is the basic format for creating a variable:

<Access modifier> <data type> <name of variable>;

Access modifiers require some higher knowledge to understand , So simply understand that writing "public" in the access modifier will enable us to access it in the Unity
data type : it is the type of data we need to store
name: we use this name to get the value .
; : this is format followed by c# ie every statement (except functions) ends with a ";"

For our case we can write like : public Vector3 move;
And note that we can write this in anywhere along with start and Update 

and we can use this in our Translate like transform.Translate(move);

Now our code will be like 



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Controller : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
    // Update is called once per frame

    public Vector3 move;
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.UpArrow)) // runs the below block only if the GetKeyDown is true
        {
            transform.Translate(move);
        }
   }
}
And now if we go back to unity we will be able to see move variable in unity


try modifying the values and click play you will be able to move it according to direction you mentioned in the move variable.
find a value that will move one road block of your game.for me it was 0.2 in y axis of move.

this was just to mention the use of variable and get a value for which it moves 1 block.

Now what we need is to make it move in any direction we click.
for that we revert our script back to our old script ie:




using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.UpArrow)) // runs the below block only if the GetKeyDown is true
        {
            transform.Translate(new Vector3(0,0.2f,0));
        }
    }
}

Now what we need to do is use the same GetKeyDown function but use different arrow keys 
and vector3 respectively
Note that while giving values of vector 3 in script , there should be 'f' at the end if the value has a decimal point in it.
So our code becomes


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.UpArrow)) // move up
        {
            transform.Translate(new Vector3(0,0.2f,0));
        }

        if(Input.GetKeyDown(KeyCode.DownArrow)) // move down
        {
            transform.Translate(new Vector3(0,-0.2f,0));
        }
        
        if(Input.GetKeyDown(KeyCode.LeftArrow)) // move left
        {
            transform.Translate(new Vector3(0.2f,0,0));
        }
        
        if(Input.GetKeyDown(KeyCode.RightArrow)) // move right
        {
            transform.Translate(new Vector3(-0.2f,0,0));
        }

    }
}



Now if you play the game you will be able to move in all 4 directions using arrow keys.

thats end of part 2 , thank you....

And don't do this 😂

via GIPHY

No comments:

Powered by Blogger.