0% found this document useful (0 votes)
85 views11 pages

Practical 1 3D Practice

The document describes a practical experiment in Unity and C# for a mobile game development course. Students are asked to create a 3D Unity project with a plane, cube, and sphere where the sphere falls and destroys the cube, displaying text. Keyboard inputs should load a new scene. The document provides step-by-step instructions for students to program interactions between 3D game objects using C#, including applying materials, physics, tags, collisions, and scene loading. Students submit their project, code, and observations.

Uploaded by

Johnny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views11 pages

Practical 1 3D Practice

The document describes a practical experiment in Unity and C# for a mobile game development course. Students are asked to create a 3D Unity project with a plane, cube, and sphere where the sphere falls and destroys the cube, displaying text. Keyboard inputs should load a new scene. The document provides step-by-step instructions for students to program interactions between 3D game objects using C#, including applying materials, physics, tags, collisions, and scene loading. Students submit their project, code, and observations.

Uploaded by

Johnny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Mobile
Mobile Game
Game Development,
Development, BTech
BTech IT
IT Sem
Sem VII
VII

Unity and C#
Part A
PRACTICAL 1
3D Practice
Topics Learned: Unity settings, C# Scripts, 3D Objects, Identify Keyboard Input, Tags, Collider,
RigidBody, Scene Management-Text, Load new Scene.

Scenario: Create a 3D Project in Unity “MyProject1” for MAC and PC. This project should have a
plane on which a cube is placed. Add material to the plane and cube. Place a Sphere with
material above the cube at a distance and when the Project is Run the Sphere should fall on top
of the cube, when it collides a Text “Destroyed” should be displayed. Pressing Key “s” should
load a new scene.

Learning Outcomes of Practical: Students would be able to


1. Understand the Unity Settings for Game Development.
2. Find and match the apt 3D components with required properties.
3. Apply Programming Concepts to make the Project interactive.

Steps to be followed:
 Open Unity Hub and under Projects create a new Project “MyProject1”.
 Once the Project is opened in Unity, set the Layout as either 2 by 3 or create your own
layout.

Step1: Adding Primitive GameObjects


 Create a plane (add material),
 Create a cube on plane (add material),
 Add a script to cube,
 Destroy the cube on start.

Step2: Destroy GameObject after 4 secs


 Create a cube on plane (add material),
 Add a script to cube,
 Destroy the cube (after 4 secs).

1|Page
Mobile Game Development, BTech IT Sem VII

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Mobile Game Development, BTech IT Sem VII

Unity and C#
Step3: Recognizing Mouse Click
 Create a cube on plane (add material),
 Add a script to cube,
 Destroy the cube when mouse clicked on cube.

Step4: Recognizing Keyboard Input


 Create a cube on plane (add material),
 Add a script to cube,
 Destroy the cube when Space Key clicked.

Step5: Adding Physics (for Gravity)


 Add Component (Physics-Rigid Body) to cube (so that if you lift the cube it falls down
due to gravity).

Step6: Adding Force (Upward Direction)


 Add Component (Physics-Rigid Body) to cube,
 Add Force to cube so when space pressed the cube moves upward direction and falls
back.

Step7: Adding Force (Upward Direction)


 Add Component (Physics-Rigid Body) to cube,
 Add Force to cube so when space pressed the cube moves upward direction and falls
back.
 The cube rotates when it jumps. If you donot want it to rotate then Cube Rigidbody ->
Constraints-> Freeze Rotation on X-axis.

Step8: Adding Velocity (Forward Direction)


 Add Component (Physics-Rigid Body) to cube,
 Add Velocity to cube so when space pressed the cube moves forward direction.

2|Page
Mobile Game Development, BTech IT Sem VII

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Mobile Game Development, BTech IT Sem VII

Unity and C#
Step9: Detect Collision
 Add Sphere on top of cube,
 Add Component (Physics-Rigid Body) to Sphere,
 Add tag “Enemy” to Sphere,
 Destroy cube when sphere with tag “Enemy” falls on cube.

Step10: Detect Collision


 Add Sphere on top of cube,
 Add Component (Physics-Rigid Body) to Sphere,
 Add tag “Enemy” to Sphere,
 Destroy sphere when sphere with tag “Enemy” falls on cube.

Step11: Add new Scene on key press ‘S’


 Create a new scene/level. Add in Scenes folder of project,
 In Build Settings check if all scenes are present,
 On Key Press ‘S’ new scene to be displayed.

Step12: Add Text UI


 Create a New Text Element UI,
 Change to 2D mode and reset the position center, font size and color,
 Add Text Element as reference to public GameObject of Text created in Script,
 On Collision delete sphere and then display text “You Win”.

Step13: Move GameObject using Left & Right Keys


 Add public speed variable,
 Add force for x and z axis,
 Freeze Rotation of cube on x, y and z axis so it only moves left and right and doesnot
rotate when moving.

3|Page
Mobile Game Development, BTech IT Sem VII

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Mobile Game Development, BTech IT Sem VII

Unity and C#

4|Page
Mobile Game Development, BTech IT Sem VII

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Mobile Game Development, BTech IT Sem VII

Unity and C#
Part B
(to be completed by students)
(Students must submit the soft copy as per the following segments. The soft copy must be
uploaded on the Blackboard. The filename should be Batch_RollNo_Exp_No)
Roll No.: I057 Name: Siddharth Somesh
Prog/Yr/Sem: MBA Tech IT/4th/7th Batch: 2
Date of Experiment: 20/07/2021 Date of Submission:

1. Program: (Write Topics Learned, Scenario, Paste your program code (C# Scripts), Project
Window, Inspector Window of Plane, Sphere, Cube object).

Topics Learned: Unity settings, C# Scripts, 3D Objects, Identify Keyboard Input, Tags, Collider,
RigidBody, Scene Management-Text, Load new Scene.

Scenario: Create a 3D Project in Unity “MyProject1” for MAC and PC. This project should have a
plane on which a cube is placed. Add material to the plane and cube. Place a Sphere with
material above the cube at a distance and when the Project is Run the Sphere should fall on top
of the cube, when it collides a Text “Destroyed” should be displayed. Pressing Key “s” should
load a new scene.

Program Code: C# Script – CubeScript

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

public class CubeScript : MonoBehaviour
{
    Rigidbody rb;

    float xInput;
    float zInput;
    
    public float abc;

    public GameObject WinningText;
    // Start is called before the first frame update

5|Page
Mobile Game Development, BTech IT Sem VII

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Mobile Game Development, BTech IT Sem VII

Unity and C#
    void Start()
    {
      Debug.Log("Project Started");
      //Destroy(gameObject,4f);
      rb = GetComponent <Rigidbody>();
      WinningText.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
    {
        Debug.Log("Key Pressed");
        //rb.AddForce(Vector3.up * 250f);
        rb.velocity = Vector3.forward * 10f;
    }

        if(Input.GetKeyDown(KeyCode.S))
        {
            SceneManager.LoadScene("Scene1");
        }

        xInput=Input.GetAxis("horizontal");
        zInput=Input.GetAxis("vertical");
        rb.AddForce(xInput * abc, 0f, zInput * abc);
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            WinningText.SetActive(true);
        } } }

Project Window:

6|Page
Mobile Game Development, BTech IT Sem VII

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Mobile Game Development, BTech IT Sem VII

Unity and C#

Inspector Window: Plane

Inspector Window: Cube

7|Page
Mobile Game Development, BTech IT Sem VII

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Mobile Game Development, BTech IT Sem VII

Unity and C#

Inspector Window: Sphere

Inspector Window: WinningText

8|Page
Mobile Game Development, BTech IT Sem VII

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Mobile Game Development, BTech IT Sem VII

Unity and C#

2. Output: (Paste Output Screens of Project played on Unity).

Output Screen: Upon Project Start

Output Screen: After Cube is destroyed

9|Page
Mobile Game Development, BTech IT Sem VII

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Mobile Game Development, BTech IT Sem VII

Unity and C#

3. Observations: A brief description of the design aspects and working of the code in your
own words.

After the successful completion of the practical, I made the following observations:

 The Cube and Sphere objects fall through the Plane unless they have Rigidbody
component added to them.
 C# script written for Cube object must be manually added to the Cube using Add
Component for the script to work.
 If the object moves past the plane that it is resting on, it can fall off of it and fall
through the floor.
 Giving colour to any object requires creating a Material which must also be
manually added to the object using Add Component.
 Unity can register mouse clicks and key presses using OnMouseDown and
GetKeyDown respectively as each keyboard key has its own name in Unity.
 For force and velocity to be added to an object, Vector3 must be mentioned in
the code to specify the amount of force or velocity being added.

4. Conclusion (Learning Outcomes): How were the outcomes defined for the experiment in
Part A fulfilled through the scenario (Write on each Learning Outcome)?

10 | P a g e
Mobile Game Development, BTech IT Sem VII

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

Mobile Game Development, BTech IT Sem VII

Unity and C#
After successful completion of the practical, I was able to make the following
conclusions:

 Understood the settings in Unity for developing a game or project including


preferences.
 Understood how to find and insert 3D components in the project like Cube,
Sphere, Plane etc.
 Understood how to apply C# programming concepts to the project to make it
interactive such as making the Cube move or the Sphere fall.

11 | P a g e

You might also like