You are on page 1of 15

Experiment: 2.

Student Name: Vivek UID: 20BCG1024


Branch: CSE, G&G Section/Group: 20BCG1, A
Semester: 4th Date of Performance:30-09-2022
Subject Name: DPG Subject Code: 20CSP-286

1. Aim/Overview of the practical: Moving, rotating & scaling Game objects and in Unity and
Add colors, textures, and Parent and child to the Game object.

2. Task to be done: In this experiment, we have to create GameObjects on Unity. We have to


add color/textures and movement to the objects.

3. Software Used: i)Unity,

ii) Vs Code

PROCEDURE:
1. Create a new project.
Fig. 1. Create a new project.

2.Create objects in Unity

First, you have to open the Unity project. Create the Plane for your game.

Click on the "GameObject" menu in the menu bar. Select 3D objects and pick the "Plane" option.
Fig. 2. Adding 3D Object Plane.

Then we add the Cube to the scene.

Click on the "GameObject" menu in the menu bar. Select 3D objects and pick the "Cube" option.
Fig. 3. Adding 3D Object Cube.

2. Add color to the objects.

We create material assets of two different colors and add it to the objects.
Fig. 3. Adding material to the objects.

3. Create C# Script.

Right click on Assets. Select Create >> C# script.

Rename the script as Movement. Write the code as the following.

A. (Jump,Horizontal,Vertical)
using System.Collections; using
System.Collections.Generic; using
UnityEngine;

public class Movement : MonoBehaviour

Vector3 Vec;
// Start is called before the first frame update

void Start()

// Update is called once per frame void Update()

Vec = transform.localPosition;

Vec.y += Input.GetAxis("Jump") * Time.deltaTime * 20;

Vec.x += Input.GetAxis("Horizontal") * Time.deltaTime * 20;

Vec.z += Input.GetAxis("Vertical") * Time.deltaTime * 20;


transform.localPosition = Vec;

}
Go back to the Unity window. Drag and drop the move script onto the cube.

Click on the Play button.

Press "Up Arrow" Key, and the Cube will move in a forward direction.
Fig. 5. Up arrow movement

Press the "Down Arrow" Key, and the Cube will move in a backwards direction.

Fig. 6. Down Arrow movement.

Press the "Left Arrow" Key, and the Cube will move to the left.
Fig. 7. Left arrow movement.

Press the "Right Arrow" Key, and the Cube will move to the right.

Fig. 8.Right arrow movement.

Press the "Space" Key, and the Cube will jump.


Fig. 9. Space bar jump movement.

B. Move and Rotate the object by Arrow key press


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

using UnityEngine;

public class Movement : MonoBehaviour


{

// Start is called before the first frame update void Start()

// Update is called once per frame void Update()

{ if (Input.GetKey(KeyCode.UpArrow))

{ this.transform.Translate(Vector3.forward * Time.deltaTime);

} if (Input.GetKey(KeyCode.DownArrow))

{ this.transform.Translate(Vector3.back * Time.deltaTime);

} if (Input.GetKey(KeyCode.LeftArrow))

{ this.transform.Rotate(Vector3.up, -10);

if (Input.GetKey(KeyCode.RightArrow))

{
this.transform.Rotate(Vector3.up, 10);

}
Press "Left & Right Arrow" Key, and the Cube will Rotate to the left and right.

Fig. 10. Object rotated.

Press the "Up & Down Arrow" Key, and the Cube will move forward and backwards.

Fig. 11. Object moved backward. Fig. 12. Object moved forward.
C. Move the object by using keys W,A,S and D.
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Movement : MonoBehaviour

// Start is called before the first frame update void Start()

// Update is called once per frame void Update()

{ if (Input.GetKey(KeyCode.A))

{ transform.Translate(0.1f, 0f, 0f);

} if (Input.GetKey(KeyCode.D))

{ transform.Translate(-0.1f, 0f, 0f);

} if (Input.GetKey(KeyCode.S))

transform.Translate(0.0f, 0f, -0.1f);

}
if (Input.GetKey(KeyCode.W))

{ transform.Translate(0.0f, 0f, 0.1f);

Go to Unity window.Click on the Play button.

Press the “W” key. The object will move forward.

Fig. 13. “W” Key movement.

Press the “S” key. The object will move backwards.


Fig. 14. “S” Key movement.

Press “D” key. The object will move to the right side.

Fig. 15. “D” Key movement.

Press the “A” key. The object will move to the left side.
Fig. 16. “A” Key movement.

LEARNING OUTCOMES:

1. Creating GameObjects in Unity.

2. Transforming objects in Unity.

3. Adding colors and textures to objects in Unity.

4. Coding C# Script gives movement and function to 3D objects in Unity.

You might also like