2
\$\begingroup\$

I got 2 problems with the game,

  1. I don't know how make my GameObject (snake head) move based in a grid for some reason I don't know it moves freely on the screen.
  2. The tail keep instantiating above the head not behind... I tried many things and did not work and didn't find an answer in the forum that helps me.

Now I'm back to the begin and looking if someone can help me.

This is the code I use to generate a grid. If anyone could help me with some code, I appreciate it.

public class LevelManagerTest : MonoBehaviour
{

    public GameObject tile;
    [SerializeField]
    private Transform gridParent;
    [SerializeField]
    public Transform[,] Grid;

    private GameObject[,] grid = new GameObject[12, 7];

    public float TileSize
    {
        get { return tile.GetComponent<SpriteRenderer>().sprite.bounds.size.x; }
    }


    void Start()
    {
        CreateLevel();
    }


    private void CreateLevel()
    {
         Vector3 worldStart = Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height));

        for (int y = 0; y < 7; y++)
        {
            for (int x = 0; x < 12; x++)
            {
                PlaceTile(x, y, worldStart);
                grid[x, y] = tile;
            }
        }
    }

    private void PlaceTile(int x, int y, Vector3 worldStart)
    {
        // creates a new tile and makes a reference to that file in new tile variable
        GameObject newTile = Instantiate(tile, gridParent);

        //uses the new tile reference and change the position of the tile
        newTile.transform.position = new Vector3(worldStart.x + (TileSize * x), worldStart.y - (TileSize * y), 0);
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ Are you asking this question for a logical help or the actual code snippets? Since the code is not relevant to your problems about this game, seems a bit confusing. \$\endgroup\$
    – Tolga Şen
    Jun 2, 2017 at 16:33
  • \$\begingroup\$ Some code, I'm creating this game to learn/improve my code skills. I know some of the logic behind it, but i don't got idea how to put them in a code... Sorry for not clearing up in the question what I wanted. \$\endgroup\$ Jun 2, 2017 at 16:40
  • \$\begingroup\$ Have you reviewed the many previous questions about implementing Snake? There's a decent chance the help you need is already present in an existing answer. \$\endgroup\$
    – DMGregory
    Jun 2, 2017 at 16:50
  • \$\begingroup\$ yeah i reviewed and i was doing this again now, the answers i tried the tail was instantiating above the head and not behind i tried to fix it but i could not and for some reason my snake head is not moving in the grid. This is why i open this, to see if someone could help me... \$\endgroup\$ Jun 2, 2017 at 17:12

2 Answers 2

3
\$\begingroup\$

You have handled the process of creating grids. Tile based movement is a bit more complex than normal 2D or 3D movement. You can find some headlines of the implementation but obviously I cannot provide you the whole code for the game.

For the movement of the snake, you can create speed variable with using Vector2.Up, Vector2.Down, Vector2.Left, Vector2.Right. For moving the snakes body, you can use Coroutine class of Unity. The main idea behind the movement is that translate the position of the head of snake directly into the next cell, wait for some duration and then start to move again, something like following:

void Start()
    {
        coroutine = Move(Vector2.Up);
        StartCoroutine(coroutine);
    }

    private IEnumerator Move(Vector2 direction)
    {
       transform.position += direction;
       //Wait time is the duration between snakes' moves
       yield return new WaitForSeconds(waitTime);
    }

The coroutine won't stop executing unless you call StopCoroutine()

After correctly implementing only head's movement, you should implement the body parts of the snake with the following logic:

Keep a reference for each block part of the snake in a list When you start moving, move the head towards direction, but other body parts must move towards the lower indexed body parts direction. For example;

bodyPartsArray[0].position += Vector2.Up;
   for(int i = 1; i < bodyPartsArray.Count; i++){
   bodyPartsArray[i].position = bodyPartsArray[i-1].position;
}

Finally, for instantiating a new body part for the tail, you must append bodyPartsArray, and position the new body part to the opposite of last body part's movement vector. (You can calculate that vector like this before you add the new body part:

//Subtract the last part's position from the previous one
Vector2 newPartsDirection = bodyPartsArray[bodyPartsArray.Count - 1] - bodyPartsArray[bodyPartsArray.Count - 2];

//Add the direction to the last part's position
Vector2 newPartsPos = bodyPartsArray[bodyPartsArray.Count - 1] + newPartsDirection;

Saying again, these are the key parts that you have asked, not the entire code or project. But I hope this would be a guideline for you, to implement the features that you wanted.

\$\endgroup\$
0
0
\$\begingroup\$

Once you have a grid, have your snake's head's position be in grid units. Move the snakes head in tile units. Then move the actual GameObject to the grid units times their width. Ex: Snake head is at (5,5) on a grid made of 1 unit wide squares, set its position to (5,5) ie: 5 grid units * 1 unity unit.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .