Skip to main content

Posts

Showing posts from 2019

How to make a first person character controller - Unity

A first-person character controller script is the starting point of any fps shooter game. Even new Unity game developers tend to write their own fps controller script before learning anything else as it is challenging and very exciting. In this article, we gonna create two simple fps controllers from scratch and will guide you step by step on how it works. First, let's understand the basic concept of an fps controller. You should have a rotating camera that rotates in the x-axis to look up and down and the character should rotate on the y-axis to look around. There are two ways to create an fps controller in the Unity Engine.  Using Character controller component Using the RigidBody component Both have different advantages and disadvantages when using them. It depends on your game to select the best fps controller according to your needs.  Character controller based fps controller Pros: Is grounded check is built-in Has its own methods to move the ...

Singleton design pattern in unity - technoob

Singleton design pattern is one of the simplest and widely used design patterns. Singleton simply means that the class cannot be instantiated and there should be only one instance of that class. The singleton instance is created using the static keyword as we need to access it without creating a new instance.  Creating singletons is a bit different from la nguage to language. But the concept is still the same.  In Unity creating a singleton is a bit different from other languages as there are scenes and we need to make sure our instance gonna persist in between every scene. How we create a singleton in Unity is pretty easy. public class ManagerClass { public static ManagerClass instance; void Awake () { if (instance == null ) instance = this ; else if (instance != this ) Destroy(gameObject); DontDestroyOnLoad(gameObject); } } We create a variable of the class called instance. In the...

Save System for Unity - Technoob Technology

Unity API has a class called ‘PlayerPrefs’ which allows saving basic data types with a string key. But when a game becomes complex ‘PlayerPrefs’ can't save all the data and it's so hard to access them as you have to remember all the keys and call one by one. To avoid this we can create a custom save system which can save a class at once which contains all the data values. We need two scripts for this save-system.             01.SaveSystem class             02.SaveData class Save system class will contain all the methods to save and retrieve data and ‘SaveData’ class is used as the base class for the classes we going to create to store data.   ‘SaveData’ class doesn’t contain any methods and not extended by any other classes or interfaces. Make sure to mark this class as ‘[System.Serializable]’ in order to make sure this is serializable. ...

Unity Native Toast - Technoob Technology

Unity engine is an awesome game engine but it doesn't contain a toast system within its API. In this article, you gonna learn how to create a toast system like the native Android toast system in Unity Engine. To accomplish this we need a tween engine and for this tutorial, we are using LeanTween which is available in the Unity asset store for free. You can change this to whatever tween engine you would like to use. Let's get started.  This module contains only a single script and pretty easy to understand. Let's call the class as 'Toast' to give a taste of what this is for. First, we need to create canvas prefab with a rounded rectangle and a text element. Then We create the script called Toast and this should be a singleton in order to keep this toast canvas in every scene without destroying. The singleton pattern is simple and what it does is check if there is another toast canvas and if there is another one destroys the current o...

Unity Get Post web requests - Technoob Technology

Web requests in unity are super easy to use. We gonna use the UnityWebRequest class as the WWW class is deprecated now. UnityWebRequest class can be used for both Get and Post methods. The 'UnityEngine.Networking' namespace is required to work with UnityWebRequests.  This tutorial is made with some simple UI elements. First Let's see how the GET method works. 01. UnityWebRequest.GET We use this method to receive data from the server as normal get requests. Using coroutines in web requests is mandatory as we have to wait until the download is complete before showing the results, and the coroutines make this much easier. A simple button calls the method and a text element will show the result. The script contains a reference to the text element and the string URL of your PHP file.  When the button is pressed the button executes the ButtonGetData method and that method simply starts the GetData coroutine. We use the using keyword as this data ...

Object Pooling system for unity - Technoob Technology

As a game developer, you might have experienced performance issues when instantiating new game objects in a  game. This is a very usual thing as instantiating an object in the middle of the game costs a severe amount of  CPU power. Professional game developers use various methods to avoid instantiating objects like this. Pooling is  the most popular method to do this. We gonna target unity engine in this tutorial and this class is created in C# language.  A pool is a set of instantiated game objects but disabled. When a class or method requires a game object of that type  pool simply takes one of the game objects from the pool and returns it. We can create pools within each class and that is going to be  very hard to manage and time wasting as you have to rewrite the same code again and again. To avoid this we are going to create a  custom class called Pool and implement all the functions a pool should contain within it.  As we...