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)
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.
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.
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 :-)
0 Comments:
Post a Comment
Thank you for your comment !