Showing posts with label First Game. Show all posts
Showing posts with label First Game. Show all posts

Tuesday, July 13, 2021

First Script in C# Unity3D (Rotate Object About Any Axis)


Hello friends , So now its time to write your first script in C# unity3D.

Before write your C# script , make sure that camera , directional light should be in there your scene.

first add an object in the scene on which you will add the script to perform some action.

so here i will add 3d object cube (see below).

If you want to zoom object select it on scene and press "F" on the keyboard.

how to add script into the object?

To make your project user friendly first make a folder named as : Script

Right click in assets window  -> create ->folder (name it Script)


Open Script folder ->create->C# Script ->Cubescript ( give any name to your script)

Other way to add script:

select game object to which you want to add script , in the inspector panel see add component and click it -> write script name (Cubescript), to attach the script to that object.

script extension would be ."cs".

Now open the Cubescript in Visual Studio to edit it.

first look for the script would be like below:


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

Above three lines are the libraries having full of mini function.

The above code shown in the above image is a by default code for any script.

void Start() function is the function which will be called for very first frame in any game/application at run time.

Every script starts wit this function.

void Update() function is the function which will be called for every frame in the game/application at run time. ( 60 frames per second)

code start from Public class Cubescript : MonoBehaviour 

MonoBehaviour is the additional library for mono develop software which unity understands.

Code blocks begins with open curly bracket " { " and end with close curly bracket " } "


1st button is for play the game scene.

2nd button is for pause and play.

3rd button is far play the game scene frame by frame (one by one).

see below code:

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

public class Cubescript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Game Started !"); // run for only first frame
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("Game Updating !!!"); // run for every //frame after first frame
    }
}

attach this script to your object.

Debug.Log is for print any thing which is inside the bracket(like in the code) in Console.

If console is not there you can add it from window -> General -> Console.



If you have done all the things properly as we mentioned above then its time to run your first script.

hit run button and see the output in console tab(see the screenshot).


Congrats! you have successfully run your first C# script.

For every object you can see Position , Rotation and Scale ARE there in Inspector window.



Now we will rotate the cube around x axis (or any axis which you want.)

 In inspector window you can see rotation is there inside the transform. so write:

transform.rotate(x,y,z);

for x, y, z we can give value 1 or 0 , 1 means object will rotate about that axis. 

0 means object will not rotate about that axis.

So rotate about x axis (here x rotation value will change in transform):

transform.Rotate(1, 0, 0);

Complete Code : 

//Object Rotate about X axis
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cubescript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Game Started !"); // run for only first frame
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(1, 0, 0);
    }
}



 Thank you :-)



Saturday, July 10, 2021

2D And 3D Objects In Unity3D

First view of our project look like below.

If not ! Then you can change your project layout from layout section in window tab.

here my layout is of 2 by 3.


In hierarchy window you can see there are two game object present already. These Game objects are :

i) Camera 

ii) Directional light

Camera 

left hand side we can see the camera, x,y,z axis and camera preview.

Right hand side we can see the inspector window for camera.From inspector window we can change the position, scale, rotation for the camera.

Sometimes we could not see the object presents inside the scene view.To see the object we can change the camera position/Projection /clipping plane near ,far of the camera.

Directional Light

we can add directional light in our game view from the game object tab ->light->directional light. From the inspector window we can change the light , intensity and more for thew directional light.

Okay ! Now we will add a 3D game object in our scene.

Here in unity we have two option to add game object in the scene.

first from the game object tab GameObject->3DObject->select any of the object from the listed objects like cube,sphere,plane,quad etc.

the other option to add 3D game object is quite simple just right click in hierarchy window select 3DObject and then choose any of the object from the list,see below.


 

After adding the cube 3D Object we can see a cube inside the game scene and inspector window for the cube from which we can change the cube properties.

For axis simply understand this:

X is for LEFT and RIGHT

Y is green on , for UP and DOWN 

Z is for FORWARD and BACKWARD

Here  we can see mesh filter and mesh renderer as component of cube.Mesh Renderer is for rendering the mesh like cube , sphere,Capsule, cylinder etc even we can download meshes from the asset store as well and can use it.

If in the Game  lighting is not proper then go to window tab -> lighting settings -> and Check auto Generate(Generate lighting).

Similarly  we can add other game object like sphere , cylinder , capsule etc in side the scene. 

Thursday, July 8, 2021

How To Create Your First Game or Project in Unity3D

Make Your First Game In Unity3D

Hello Friends , as of now we have installed Unity3D. Now its time to create our first unity game!

Here we can create 2D or 3D games and steps to create both are same. 

So ,Finally we go for 3D game, follow the steps:

Step1: Click UnityHub and open it.

Step2: Click NEW


Step3: Select 3D and give some project name .Here i have given First_Game and choose the desired location.

Step4: Click CREATE !

The first view window of your project will look like below:


Now we will see some window and terms which are present in above screen.

In above screenshot there are 4 numbered window.We will look into all one by one.

1. Project and console window

2. Hierarchy window

3. Scene and Game window/view

4. Inspector window


Project and console window

The Project window displays all of the files related to the Project and is the main way by that a user can navigate and find Assets and other Project files in the application.

When user start a new Project by default it will be open. 

If you can't see the any of window then you can add it by following steps show below in the image. 

 

Hierarchy window

The Hierarchy window displays every GameObject in a Scene, such as models, Cameras, or Prefabs.

User can use the Hierarchy window to sort and group the GameObjects you use in a Scene. When user add or remove GameObjects in the Scene view, he also add or remove them from the Hierarchy window.

The Hierarchy window can also contain other Scenes, with each Scene
containing their own GameObjects.

Scene and Game window/view

The Scene view is the interactive window/view into the project/game you are creating.

You can use the Scene view to select and position Object,characters, Cameras, lights, and all other GameObjects.

The Game view is rendered from the single or multiple cameras in your project. It represents the final and published project/game. You need to use one or more Cameras to control what the player sees when they are using your project/game or final application.

Inspector window 

Inspector window is basically used to view and edit properties of GameObjects,Unity components, Assets, Materials,and settings for almost everything in the Unity Editor.

    We will know more about these windows in upcoming post.In next post we will continue with the project First_Game.