0
\$\begingroup\$

Im currently learning Unity3D and I would like to recreate the old Nokia game "Snakes" (example video: https://www.youtube.com/watch?v=FXg3jVG-tXs&ab_channel=aantv, play it at 1:30 and notice the movement and even the animations when the snake is turning to the sides).

The game movement is grid based as you can see, so the snake can turn only, when the head is in the center of each floor slab. I have done this by using Coroutine and I am interpolating the position of the head between the distance of each 2 slabs. (example video: https://www.youtube.com/watch?v=_LWYQE1UC68&ab_channel=Chlebikpastika).

My question is, how to design and implement the solution using procedural mesh? My idea is to update the vertices of corresponding snake body part with the values of vertices of previous snake body part. I could have Body class, which could have the buffer of vertex positions of previous body, and after each update, the vertices would be updated with currently poped values from buffer. I dont know if this is good design, do you have any ideas how I could achieve the movement of vertices of the snake?

\$\endgroup\$
4
  • \$\begingroup\$ The approach I'd use is a "ring buffer" aka "circular array"... Basically an array at least as long as your snake and a position index that wraps around. Every time the snake meets a new square, add it to the array at i then increment i by 1. Keep repeating for new segments. If i > array length, set i = 0. When you want to get current snake body positions, make a note of i then decrease by 1 and you have the neck location. Subtract 1 again and it's the next segment. Keep doing until you've reached the array length. Obviously if i < 0, set i = length -1. \$\endgroup\$
    – Basic
    Nov 1, 2020 at 11:30
  • \$\begingroup\$ Do you already know the basics of how to create procedural meshes in the first place? \$\endgroup\$
    – Philipp
    Nov 4, 2020 at 14:40
  • \$\begingroup\$ I know I have to body parts of snakes using polygons, so I have to define vertices and triangles and use Mesh class in Unity. To make snake move, I need to update positions of vertices in a way, that the body will follow the head. Im I right? \$\endgroup\$
    – Breadman
    Nov 4, 2020 at 14:48
  • \$\begingroup\$ "Am I right" is usually not a useful question here. If you have enough information to try it, then try it. When you run it, you'll see concrete proof of whether your strategy worked. If you don't have enough information to try it, then identify what the missing piece is and ask for that. \$\endgroup\$
    – DMGregory
    Nov 4, 2020 at 14:56

0

You must log in to answer this question.

Browse other questions tagged .