22.1 C
Kathmandu
Monday, July 26, 2021

Currently in beta testing - RECAP Token. Interested? Join here!

Game Programming Series: Modify the cube with Unity3D

  • N/R
  • N/R

Must read

Review Capital: How To Participate in Testnet phase? Updated Tokenomics

How to participate in Testnet Phase of BuzzerNepal? To participate in Testnet phase of BuzzerNepal, please request testnet RECAP...

Rice, a peacemaking merchandise

Rice, is an edible starchy edible grain. It is the leading cereal crop in the world. Rice is the world's third on...

EURO 2020: Hungary stun France

Hungary stunned the World Champions after the host held France to a 1-1 draw at fan-filled Ferenc Puskas Stadium in Budapest. Antione...

High Mucormycosis cases reported in India

India has reported about 12,000 cases of "black fungus", mostly in patients recuperating from Covid-19. This severe infection is...

First of all , create a new project in unity. I am using 2019.4.15f for my version here. In your project hierarchy you should see just the Main Camera and the Directional Light. Leave them as they are. Now right click on project hierarchy and from 3D Object > Cube. 

With the cube selected in the window, go to Transform, and select those three dots and press reset. 

Now on your project window , inside the assets folder,  right click to get to the Create>Materials. Select name for the material say : CubeMaterial.

Now we will go into coding to add functionalities in the cube for it.  To create a code , click on the Cube and click Add Components > New Script and type say Cube. Now double click the cube.cs script and it opens the Visual Studio for you. In the code, we will add a public variable called speed which will define the speed at which the cube rotates. For now we  will set it to 10.0f; Also we will add an array of colors called color which consists of 3 colors : Red, Blue, and the green. We will also add a public MeshRenderer called as Renderer, which is used to access the materials like color of the cube at the beginning of the scene. 

At the start of the screen , we want the cube to appear the certain position within the x,y and z axis. To change that lets use the transform.position to change the position to (3,4,1) . Similarly we can change the size of the cube within the code by using transform.localScale to be equal to the product of Vector3.one and 3f; So now the cube is 3by3by3 units. We can also select material property to start with the blue color. First we define material variable of type Material whose value equals to the Renderer.material. and then we set material color to be blue initially. 

Now if you save your code and press play on the unity editor. You will see on the game screen with a blue static cube. But we want the cube to rotate and change color as we press the space bar. To do that we will write the rest of the code in the update portion of the Cube.cs code. So in the Update portion, for us to randomly change the color we will the Random Color selector. To do that lets create a changeColor variable of type Color . Since we have already defined mycolor array at the begining of the code, we can use that to make that selection. So we will have changeColor equals to myColor[Random.Range(0,3)]. 

Now to rotate the cube , we will use transform.Rotate function, and it will be as Transform.Rotate(100.0f *Time.deltaTime, 0.0f,0.0f) . 
For us to change color on pressing the spacebar, we need to add the following code which checks to see if the Spacebar is pressed or not. 

Now with all the additions to the code , save the code and press the play button at the top of editor and you should  see a rotating cube that changes color as you press the cube. 

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article

Review Capital: How To Participate in Testnet phase? Updated Tokenomics

How to participate in Testnet Phase of BuzzerNepal? To participate in Testnet phase of BuzzerNepal, please request testnet RECAP...

Rice, a peacemaking merchandise

Rice, is an edible starchy edible grain. It is the leading cereal crop in the world. Rice is the world's third on...

EURO 2020: Hungary stun France

Hungary stunned the World Champions after the host held France to a 1-1 draw at fan-filled Ferenc Puskas Stadium in Budapest. Antione...

High Mucormycosis cases reported in India

India has reported about 12,000 cases of "black fungus", mostly in patients recuperating from Covid-19. This severe infection is...

Truck driver Crashes into cyclists racing for charity

A pick-up driver truck has left six cyclists in a critical condition as the truck drove in to the cyclists who had...