0
\$\begingroup\$

I am currently trying to make a snake game with Unity. I'm a beginner at C# so I decided to make a game that seemed easy. With the help of this tutorial How to make Snake in Unity (Complete Tutorial) 🐍🍎 I made a basic snake game. The problems came when I tried to change the sprites.I somehow managed to figure out how to turn the head in the direction that the snake is moving, but I cant figure out how to move the body in the right direction. I've been stuck on this for days.

Here is an example so you can understand better what I mean: enter image description here As you can see, everything works besides the body rotation. I tried to follow this tutorials Making a Snake with Snake Like Movement in Unity (Part 1/2): Follow the Leader / Synchronized Motion, Making Snake in Unity: Snake Body Corners (Unity Tutorial for Beginners) but i couldn't make the body rotate.

Here is my code so far:

private List<Transform> segments = new List<Transform>();
public Transform segmentPrefab;
public Vector2 _direction = Vector2.right;
private Vector2 input;
public int initialSize = 4;
public float rotationSpeed;

private void Start()
{
    ResetState();

}

//controls
private void Update()
{
    if (_direction.x != 0f)
    {
        if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
        {
            input = Vector2.up;
        }
        else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
        {
            input = Vector2.down;
        }
    }

    if (_direction.y != 0f)
    {
        if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
        {
            input = Vector2.right;
        }
        else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
        {
            input = Vector2.left;
        }
    }
}

private void FixedUpdate()
{
    //Body Movement 
    for (int i = segments.Count - 1; i > 0; i--)
    {
        segments[i].position = segments[i - 1].position;
    }

    //Snake Movement
    if (input != Vector2.zero)
    {
        _direction = input;
    }

    float x = Mathf.Round(transform.position.x) + _direction.x;
    float y = Mathf.Round(transform.position.y) + _direction.y;

    transform.position = new Vector3(x, y);

    //Head Rotation
    if (_direction != Vector2.zero)
    { 
        Quaternion toRotation = Quaternion.LookRotation(Vector3.forward, _direction);
        transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
    }
}

//Add body part 
public void Grow()
{
    Transform segment = Instantiate(segmentPrefab);
    segment.position = segments[segments.Count - 1].position;
    segments.Add(segment);
}

//reset game
public void ResetState()
{
    _direction = Vector2.right;
    transform.position = Vector3.zero;

    for (int i = 1; i < segments.Count; i++)
    {
        Destroy(segments[i].gameObject);
    }

    segments.Clear();
    segments.Add(transform);

    for (int i = 0; i < initialSize - 1; i++)
    {
        Grow();
    }
}

//grow or Reset
private void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag("Food"))
    {
        Grow();
    }
    else if (other.gameObject.CompareTag("Obstacle"))
    {
        ResetState();
    }
}

//body rotation


}

Due to the fact that I'm a beginner, some code would be useful. Thank you!

\$\endgroup\$
2
  • \$\begingroup\$ You will incur a lot of memory overhead using a list for the snake. Too much add/remove This is fine for a simple game like snake but not practical for larger games. \$\endgroup\$
    – user122973
    Feb 19, 2023 at 6:29
  • \$\begingroup\$ And the lazy easy way would be to make the segments a square. That way the orientation does not matter \$\endgroup\$
    – Zibelas
    Feb 19, 2023 at 11:40

1 Answer 1

0
\$\begingroup\$

Each segment needs to inherit the rotation along with the position:

segments[i].position = segments[i - 1].position; //existing line
segments[i].rotation = segments[i - 1].rotation; // added line
\$\endgroup\$

You must log in to answer this question.

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