Showing posts with label GameObject in Unity3D. Show all posts
Showing posts with label GameObject in Unity3D. 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 :-)



Thursday, July 8, 2021

Important Tab And Field In Unity3D

Menu And Fields In Unity3D

Before we start our First_Game, we will have a look into some basic and important tab and field present in the unity3D.


1. File

2. Edit

3. Assets 

4. GameObject

5. Component 

6. Window

7. Help

File:


By file , we can create new scene in our project or we can open and add already existed scene inside to our project.

by this we can a project which is already present and we can save our performed task and project.(crl+s)

we can change the build and player setting from the file  option.

After completion  of the project we can make application file of the project by build and run option presents inside the file tab. 

by this file we can install our application created by unity in any of the system.

Edit:

  

By edit we can change the project settings , preferences and also objects and prefabs created in the project (we will discuss about object and prefabs in later posts).

Assets:


By Assets, we can create assets inside the unity. Here we can import assets from our local system our from assets store (custom package).

We can download assets like 3d model, prefabs , terrains etc. from assets store or from anywhere else and can import it to our project.

we can also export the assets presents in the project.

many excited things will be there inside this which we will learn slowly in AR - VR section(so stay tune !) 

GameObject


By GameObject, we can create 2d objects like Sprite , Sprite Mask etc and 3d objects like Cube ,Sphere ,Capsule ,Plane ,Quad ,Text) game object for our project.We can create an empty object as well just by selecting create empty.

Here we can add effects  like Particle effect, Trailing, Line.

Light (Game Object): By this we can add directional light , area light , point light etc. in our project scene.

UI: By this we can add image , button , canvas , text , slider , scrollbar etc in out scene.

We can add audio and video fie from game object.(fore this we will add audio / video source from the component , don't worry we will see example for this a well). 

Component


By this we can add component to our game object.Component like audio / video source, collision collider component , rigidbody , rigidbody2D, Physics, script, UI etc. 

Window


By this we can change the layout of our scene to 2 by 3,4 split,Tall ,Wide

,or to default it self( its better to try it yourself and see the differences among all).

We can add window like game,console,Hierarchy inspector.

By this tab we can add assets store as well.By assets store we can find our useful assets, download it and can import it in our project.

During the uses of MRTK(Mixed reality toolkit) we can use XR settings and Holographic emulation so that we can see the play mode inside the head mounted display like HoloLens.

From this we can change general, animation , audio setting and also light settings through rendering option.

Help


Here we can look into about Unity,Unity Manual.

from help we can check Unity Update,report a bug and can also manage the license.

Thank you !

In the next post we will see the different type of objects and add them into our scene and will perform some task on them. :-)