Skip to main content

Posts

Showing posts with the label Unity Engine

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 ...

How to make an Event-System for unity - Technoob Technology

First, What is an Event-System? An Event-System is a collection of scripts we use to send messages between game objects without linking them through the Unity inspector. The best use of this is when you want to connect a game object of the scene to a prefab. Unity Engine doesn't allow us to do that through the inspector and the best way to get this done is by using an Event-System. What if you have 50 enemies in the game and you want to notify them all when the player dies. Without an Event-System we have to connect all the enemies to a manager or some kind of a script to do that... Think about having an array of 50 enemies. This can be solved easily using an Event-System. We will do a basic example of using this event system in the end of the article. Well, now we know the advantages of using an Event-System. Lets start coding our own simple Event-System. First we have to get an idea on how this works. First, a script in the scene raises an event using a static method o...