Intro to Unity

Joseph Harwood
3 min readJul 5, 2019

--

Unity is a cross-platform game engine developed by Unity Technologies. It is one of the most popular game engines in the world used by major and indie game studios alike. The best part about it is that is free and easily accessible for anyone that is interested. This blogpost will explain why Unity is useful, show a brief introduction to the software, and show some code that you can implement in your first project.

The key benefits of Unity:

  • Allows for Fast Prototyping
  • Asset Pipeline — Users can simply drag and drop assets from other applications seamlessly, allowing users to quickly use and update them
  • Visual properties can be modified directly without editing the code
  • ⅓ of Unity users use Unity for non-game uses like for prototyping architectural idea, interactive art installations, and data visualization
  • Multi-platform support: Mobile, Desktop, Web, Consoles

Unity programming languages:

  • C#: Allows complete and precise control. Most components are handled manually. Unity is programmed in C#. USE THIS
  • Soon To Be Deprecated: Unity Javascript (UnityScript) and Boo
  • Unity Javascript (UnityScript): Good for novice programmers, most popular
  • Boo: Python-like syntax. Least popular

The beauty of Unity is that you aren’t just programming the whole time. Building a game is a combination of using the Unity UI to create and manipulate game objects, and writing scripts to create the game logic.

Numbers correspond to the numbered list in the breakdown

Unity Engine Breakdown:

  1. Scene View — where the game is constructed

2. Game View — shows the user a preview of the game that they built. Starts with the Main Camera and Directional Light

3. Hierarchy — contains a list of all the GameObjects in a scene. Users can assign parent-child relationships between GameObjects

4. Project Browser — contains all of the assets for the project

5. Inspector window — where the details of the GameObject are viewed and can be edited

6. Toolbar — use to manipulate GameObjects

7. Play Buttons — Start and stop buttons to play the game. Step button lets you go through the game frame by frame

8. Miscellaneous Editor Settings — Collaboration and customize Layouts

A great tutorial to create your first game:

I completed this tutorial and added the ability to jump and shoot to make the game a little more interesting. Here is the code you need in the PlayerController.cs:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerController : MonoBehaviour
{

public float speed;
public Text countText;
public Text winText;

private Rigidbody rb;
private int count;

public Vector3 jump;
public float jumpForce = 2.0f;
public bool isGrounded;

public GameObject shot;
public Transform shotSpawn;
public float fireRate;
private float nextFire;

void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
jump = new Vector3(0.0f, 2.0f, 0.0f);
}

void Update() //called once per frame
{
//jump
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{

rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}

//fire
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}


}

void FixedUpdate() //physics
//movement
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

rb.AddForce(movement * speed);
}

//pickup
void OnTriggerEnter(Collider other) //collision
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}

void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 9)
{
winText.text = "You Win!";
}
}

//jump
void OnCollisionEnter(Collision collision)
{
isGrounded = true;
}

}

I also made a new script called DestroyByBoundary.cs that requires you to build an empty GameObject around the game-playing area that will destroy what is getting fired so that it won’t slow the game down:

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

public class DestroyByBoundary : MonoBehaviour
{
void OnTriggerExit(Collider other)
{
Destroy(other.gameObject);
}
}

I hope that this introduction to Unity will inspire you to get out there and create a cool game!

Sources:

https://web.wpi.edu/Pubs/E-project/Available/E-project-030614-143124/unrestricted/Haas_IQP_Final.pdf

https://www.raywenderlich.com/772-introduction-to-unity-getting-started-part-1-2

https://www.raywenderlich.com/770-introduction-to-unity-getting-started-part-2-2

--

--

No responses yet