Unity Tilemap Tips

In order to make my awesome JRPG I needed some sort of Tile System to create awesome maps. I have some code to load data from Tiled, however, Unity recently added their own system and doing it in the editor itself is much cleaner in my opinion than doing it in an external tool. However, apart from the Manual, there are not a lot of tutorials on how to use it and I found the manual pretty sparse.

The way I wanted to do it is the way I had previously set out with Tiled:

Essentially I wanted there to be 3 layers. Background, Middle and Foreground. However, the docs were not great in showing a way to do that, and inevitably you were not able to set a layer per tile like I had been doing in the past when loading from Tiled. So looking back at Tiled itself, I realised the layers were actually 3 tilemaps. The solution in Unity; create 3 Tilemap objects

 By creating 3 maps and then using the sorting layer to decide the draw order I could essentially have 3 layers like I did previously.

Similarly, as the player character only interacts with the middle layer, I only needed the physics object to be on the middle layer gameobject.

 

The last thing that did catch me out, and is a bit of a pain is having to go through is setting up if the tile is “Walkable” or not.

You essentially have to go to each individual tile asset and select the collider type. Which makes sense to be fair.

There are a load of awesome brushes as well you can grab from here:

https://github.com/Unity-Technologies/2d-extras

Which includes placing Prefabs and Animated Tiles. You do need to grab the Beta of Unity though which is less than ideal.

Very Simple Obscuring Object Culling

This little trick is from something that I worked on that involved robots. So I added the Robot from Infinite Warfare (a game which I have, but need to actually finish… like all my other games!). Essentially there was a character that moved around an environment and attacked enemies. The problem was he sometimes got obscured by the environment. As it was a prototype, I made the quickest and dirtiest thing I could and whacked it in.

As the diagram shows above I raycasted from the camer to the player character. If the raycast hits anything marked “Obstacle” or equivalent term the object is deactivated.

This is SUPPPEEERRR basic. There are some cleverer things you can do like do a box cast and seeing how much an object is inside the box cast and fade it out, but if you need to get somehting up and running quickly then this is a quick and dirty implementation.

Creating a Cloak Effect

It has been a while since I have done any type of tutorial or any technical related blog post, so I thought I would share the process of how I implemented a “cloak” effect on my character. Essentially in my game, I am adding a cloak skill and in order to visualize this to the player I want a sort of holographic semi-transparent cloak effect. Something magicy and Sci-Fi. So I jumped back into my shader coding.

If you need a quick refresh of the make up of shaders in Unity then here are a couple of previous posts (with horrendous typos that I should probably sort):

Unity Shader Tutorial – A look at a basic surface shader

Unity Shader Tutorial – a look at a basic vertex and fragment shader

Cool, let’s do it.

First we start by creating a Surface Shader and then strip it back so it just performs diffuse lighting, a good starting point for all shader creation.

basicshader

Firstly in our properties section we are going to add… well… a new property

properties-capture

Which will expose the following “_DotProduct” float value to the editor that we add in our CG Program.

 

dotproduct

We also want to add a _MainTex property value pair into our shader.

We also want to change the tags.

tags

This will render the shader in the transparent rendering queue and will ignore projectors. If we want, as the player character is becoming invisible, we can also add the “ForceNoShadowCasting” tag that will mean the object does not cast shadows.

We also want to disable the expensive PBR in this shader and switch it out for Lambertion reflectance before telling the CG program this is a transparent shader and then finally disable the lighting. Wow that was a long sentence.

pragmas

The last thing we want to do before we actually jump into our surface function is change what actually gets run into the shader. In this case we want the world normal and view direction as well as what is already there.

inputstructure

Cool, now we whack this code in the surface function.

And we get this effect:

basic

On a sphere it looks pretty basic although it makes a cool atmosphere effect one, similar to the one I used in my seasonal globe:

What this shader is doing is actually showing the silhouette of the object. If I swap the circle out for something else and we move around it we can see the outline changes.

silhouette

After I tweaked the values a bit and applied it to my asset store model, here is what I got:

cloak

The gif doesn’t do it as much justice as in-game, but I actually think this is quite cool and that shader is cheaper than the previous refraction shader I was using.

I also had a little experiment for fun with my enemy model by adding a wave to all the verts and made what I am calling the “Demontor” shader

demontor