Skip to main content

2D dynamic mesh snake in unity

This post is about me starting a project with snake character in Unity3D and finally realizing how bad of idea it was. This is a final product of the project:


The idea was to make a snake out of segments and generate a continuous mesh on top of it, in this fashion:
Design assumes each segment to have its own rotation and rigidbody2D and to be connected with Hinge Joint 2D to previous segment, with controllable player head being the first one. I recomend using Catlike Coding tutorial for generating basic mesh
First step I took was to generate one quad per segment on its position. Because Mesh generating script was attached to snake's head and I didn't bothered with rotation at first, it gave really nice, unusual effect. It's probably the best it looked during the whole project.


Rotation was fixed by simply copying rotation transform from each segment to its quad. And it still looked cool.


Then I proceeded with filling the gaps with new geometry using existing verices of neighboring quads. And snake suddenly became really dull. I didn't like the effect and quads with filled-in gaps between them were not the perfect way to simulate wrapping up snake. No idea why I didn't question it beforehand.


The image shown at the top of the post is the same snake with some values tweaked. 

In case someone will want to mess around with it, this project is made in unity 5.6.2f1 and is available for download here.
I didn't bothered with lot of things, including transforming vertices instead of creating new mesh every frame, so look out for that.

Comments