Sitemap

Kick start Unity

3 min readFeb 4, 2021

In this blog we will be learning how to create a little 2D game.

This is focused towards absolute beginners , want to try Unity.

Step1: Open Unity Hub -> Create a new project with 2D template .

Step2 : Import assets just by drag and drop.

Step3: You can create 2D assets inside Unity .

Press enter or click to view image in full size

From the assets take the Ground and Tank and place like this.(or you can create your own models like shown in the picture).

Press enter or click to view image in full size
Tank settings
Press enter or click to view image in full size
Bullets settings
Press enter or click to view image in full size
Bird settings

Create a folder “Prefabs” and drag the bullets and bird objects in that folder to make a prefab out of them.

create a C# script “movement” and add it to Tank game object .

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class movement : MonoBehaviour{public GameObject bulletprefab;public Transform firePoint;public float speed=3f;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){float horizontal = Input.GetAxisRaw("Horizontal");transform.position+=new Vector3(horizontal*speed*Time.deltaTime,0f,0f);if(Input.GetMouseButtonDown(0)){fire();}}void fire(){Instantiate(bulletprefab,firePoint.position,Quaternion.identity);}}

Create a C# script “destroy” and add it to wall gameObject.

using System.Collections;using System.Collections.Generic;using UnityEngine;public class destroy : MonoBehaviour{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}private void OnTriggerEnter2D(Collider2D other) {if(other.tag=="Bird"){Debug.Log("Bird");other.gameObject.SetActive(false);}}}

Create an empty game object and place it outside of camera boundary.

Create a C# script and add to the empty gameObject in my case i have named it “Spawn”.

using System.Collections;using System.Collections.Generic;using UnityEngine;public class spawn : MonoBehaviour{public GameObject birdPrefab;public float interval=0.3f;float time=0f;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){time+=Time.deltaTime;if(time>interval){Instantiate(birdPrefab,new Vector3(transform.position.x,Random.Range(3.25f,4.29f),0f),Quaternion.identity);time=0;}}}

Now we have to create two scripts for bird and bullets .

Create a C# script and add it to bird prefab or you can do this step before converting the gameObject as prefab as well .

using System.Collections;using System.Collections.Generic;using UnityEngine;public class bird : MonoBehaviour{public float speed=3.5f;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){transform.position-=new Vector3(speed*Time.deltaTime,0f,0f);}}

Similarly do for bullets prefab.

using System.Collections;using System.Collections.Generic;using UnityEngine;public class bullets : MonoBehaviour{public static bullets instance; // Creating a singleton patternprivate void Awake() {instance=this;}private float speed=4f;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){transform.position+=new Vector3(0f,speed*Time.deltaTime,0f);}public void OnTriggerEnter2D(Collider2D other) {if(other.tag=="Bird"){print("Bird encountered");gameObject.SetActive(false);other.gameObject.SetActive(false);}}}

And this should work just fine.

You can download the project from https://github.com/49paunilay/KickStart-Unity-Medium-blog-Tut

Follow me for more Unity , Django , Python content.

============Upcoming contents==============

Deploy ML model in Django .

Create a multiplayer room in Unity.

=====================================

Also you can follow me on Linkedin if you want->https://www.linkedin.com/in/nilay-paul-062521186/

Bird Artist =>https://www.linkedin.com/in/rakesh-mohata-6b2a70176/

--

--

No responses yet