Skip to content
Adam Graham edited this page Jul 5, 2023 · 18 revisions

Links

FAQs


Is the source project free to use?

Yes, you are free to use the project for any purpose. However, keep in mind that copyright and/or trademark laws can still apply to the original material since many of the games in my tutorials were not originally authored by me. I open source my own code for the community to learn from, but the project is intended for educational purposes only.


The ghosts are running into the wall instead of turning

If the ghosts are not turning, this implies a problem with the node system since the ghosts only change direction when touching a node. First, make sure you don't have any missing nodes on any of the corners. Double check your Node prefab has a collider component with the Is Trigger property checked on.

One of the easiest ways to cause this problem is by turning off the "Nodes" game object entirely instead of disabling only the Tilemap Renderer component. The tilemap is only used as a visual debugging aid for the nodes, so we disable it for the actual game. However, if you disable the entire game object, this also disables the Node scripts from running which is needed for the ghosts to move properly.


The pellets are not disappearing when eaten

This problem is usually caused if you have assigned a sprite to the pellet tile asset(s). We assign the sprite to the SpriteRenderer component on the pellet prefabs but not to the pellet tiles themselves. Since a copy of the pellet prefab is instantiated for each tile, we let that object handle the rendering instead of the tilemap. If we assign a sprite to the tile asset, then it will show up permanently on the board since the code only disables the game object rather than changing the tilemap.

It's also possible that you added the pellets to the wrong tilemap. In the tile palette editor, there's a dropdown for Active Tilemap where you can select which tilemap you want to draw your tiles on. Make sure to choose the correct tilemap before drawing your pellet tiles. You can even disable the Tilemap Renderer component for your pellets tilemap to ensure no rendering is being handled by the tilemap.


How do I add scoring and lives?

I did not cover this in my tutorial video because the video was already so long, but I did include the implementation for scoring and lives in the full project source code: https://github.com/zigurous/unity-pacman-tutorial. Most of the code for this is handled in the GameManager.cs script, if you want to take a look at how I handled it. You can also download the full project and open it in Unity.


How do I install the '2D Tilemap Extras' package?

Documentation: https://docs.unity3d.com/Packages/[email protected]/manual/index.html

Editor method:

  1. Navigate to the menu Edit > Project Settings, then select the Package Manager settings.
  2. Check on the box called Enable Pre-release Packages.
  3. Navigate to the package manager from the menu Window > Package Manager.
  4. Change the dropdown to Packages: Unity Registry.
  5. Find the package called '2D Tilemap Extras' and click the Install button

Manual method:

  1. Open the file Packages/manifest.json in a text/code editor
  2. Add a new entry "com.unity.2d.tilemap.extras": "2.2.0", alongside the other packages
  3. The package should be automatically installed the next time you open the Unity editor

Should I use sorting layers or z-position?

In any game, objects are rendered in a particular order. We usually want objects farthest from the camera, such as the background, to be rendered first, thus all other objects are rendered on top of it. There are 3 common ways to change the render order of objects, with the following priority:

  1. Sorting Layer
  2. Order in Layer
  3. Distance to Camera (Z transform)

The "Sorting Layer" and "Order in Layer" are properties available on the SpriteRenderer component.

I've mistakenly used the z-transform to set the render order in some of my tutorials (including Pacman) simply because I am more accustomed to 3d workflows that don't use sprite renderers. As listed above, it's better practice to use "Sorting Layer" and "Order in Layer" if available.


Error: NullReferenceException

This is a very common error in Unity and is caused for many reasons. Many times it's caused because references to other objects were simply forgot to be assigned in the editor, thus when you try to access that reference in code, it causes this error.

However, there is a specific race condition in the original tutorial code that also causes this error. A race condition occurs when the outcome of something depends on the order in which the code is executed. When I wrote the original code, the script execution order worked in my favor so I didn't notice the problem, but others might run into this problem.

In the Awake function of the GhostBehavior.cs script, we disable the behavior using enabled = false. This triggers the OnDisable events to occur which may reference other objects. Depending on the order in which the scripts get executed, some of these references may not have been assigned yet, thus causing the error. The line of code enabled = false is not needed and should be removed to prevent this race condition.