Author: Mark

  • Animating in Unity

    This example is to make a constant animation loop endlessly (say a shining sun animation). The first thing you need is a spritesheet which is basically all the animation frames in 1 image. I usually do this in Flash. When you’re done, drag the spritesheet image into Unity. In the Inspector change ‘Sprite Mode’ to…

  • Demonfall Week 4 Progress

    Decided to leave the tutorial levels till later and do something more fun – adding the demon levels. The first demon’s lightning attack is fully functional. Lightning randomly appears on the screen and it’s size,number and frequency gradually increase as your score increases. Touching it will shock you causing you to be unable to move…

  • Making a singleton in Unity

    What is a singleton? Basically, it’s code that can be used by everything else. This means you can share code between scenes in Unity. I usually use this to make sharable GUI styles. This is the basic set up: public class singleton : MonoBehaviour {     private static singleton instance;     void Awake()     {         instance = this;     } Now you can specify variables and functions here that can…

  • Using Icon Fonts in Unity

    In this example I’m using Font Awesome. First you download it and then drag the .ttf font file into your Resources folder. Now, there are 2 ways to add text: in the UI and in code. Let’s go through both. Adding icons in the UI Say you had a button in the hierarchy with a…

  • Demonfall Week 3 Progress

    This week the wing item got added in and it works too. Simply tap the middle of the screen to use up the wings when you have it and you’ll slow down your fall. The wings also animate and disappear after use. The platform’s speed is increased by the score and the guy returns back…

  • Delaying code in Unity

    Here’s an example: IEnumerator functionName(){         print (“0 – start”);         yield return new WaitForSeconds(1);         print (“after 1 second”);         yield return new WaitForSeconds(1);         print (“1 more second later”);         yield return new WaitForSeconds(2);         print (“2 more seconds after that”);         // etc     } The important things is to use IEnumerator. You can put in any number in the brackets for WaitForSeconds. Everything after the WaitForSeconds will not execute until…

  • Making reusable functions in Unity

    One of the core fundamentals of programming. Here’s an example: public void randomX(GameObject obj){         GameObject wall = GameObject.Find (“wallLeft”);         Vector2 fullScreen = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width,Screen.height));         //world point calculations         float wallWidth = wall.GetComponent<Renderer> ().bounds.size.x;         float objWidth = obj.GetComponent<Renderer> ().bounds.size.x;         float widthRange = fullScreen.x – wallWidth-objWidth/2;         float ranX = Random.Range (-widthRange, widthRange);         obj.transform.position = new Vector2 (ranX, obj.transform.position.y);     } This function gives a game object a random x position on the screen. It takes into consideration padding on the sides (in this case, a wall on both sides) as well as the width of…

  • Doing Consistent Jumps in Unity

    So in Demonfall I wanted the guy to fall, hit the platform, jump back up to the top and then fall again. I wanted the same height jump each time. This doesn’t work by default when you turn the global gravity on. The guy’s first jump would be very short (because he fell from the…

  • Demonfall Week 2 Progress

      So, we got the left and right GUI Repeat Buttons buttons working now (the dark blue buttons with the <- and -> text in them. They’re actually more of a semi-transparent grey but they’re on top the 2 blue blocks marked with a X which are the walls. I will probably have to increase…

  • Accessing code from other scripts in Unity

    For example in one script we store the number for the score as well as the function to update the score on screen. In this example it’s called gameCode.cs and it would go something like this: public int score = 0; public void updateScore(){         GameObject labelScore = GameObject.Find (“labelScore”);         labelScoreText = labelScore.GetComponent<GUIText> ();         labelScoreText.text = “SCORE: “+score /*score.ToString ()*/;     } To access this code in another script, you would do something…