1
\$\begingroup\$

I want to know:

  1. how to rotate snake's head with movement
  2. how to make tail prefabs (body) follows head direction.

snake's head tail prefab

public class Snake : MonoBehaviour {

    bool ate = false;
    bool isDied = false;
    public GameObject tailPrefab;
    Vector2 dir = Vector2.right;
    List<Transform> tail = new List<Transform>();

    void Start ()
    {
        InvokeRepeating("Move", 0.3f, 0.1f); 
    }

    void Update () {
        if (!isDied) {
            if (Input.GetKey (KeyCode.RightArrow))
            {
                dir = Vector2.right;
                //transform.rotation=Quaternion.Euler(0, 0, 180);
            }
            else if (Input.GetKey(KeyCode.DownArrow))
            {

                dir = -Vector2.up;    
                //transform.rotation = Quaternion.Euler(0, 0, 90);
            }
            else if (Input.GetKey(KeyCode.LeftArrow))
            {

                dir = -Vector2.right;
                //transform.rotation = Quaternion.Euler(0, 0, 0);
            }
            else if (Input.GetKey(KeyCode.UpArrow))
            {

                dir = Vector2.up;
                //transform.rotation = Quaternion.Euler(0, 0, 270);

            }
    }

    void Move() {
        if (!isDied) {
            Vector2 v = transform.position;

            transform.Translate (dir);

            if (ate) {
                GameObject g = (GameObject)Instantiate (tailPrefab,
                                  v,
                                  Quaternion.identity);

                tail.Insert (0, g.transform);

                ate = false;
            } else if (tail.Count > 0) {    
                    tail.Last ().position = v;

                    tail.Insert (0, tail.Last ());
                    tail.RemoveAt (tail.Count - 1);
            }
        }
    }

    void OnTriggerEnter2D(Collider2D coll) {
        if (coll.name.StartsWith("Food")) {

            ate = true;

            Destroy(coll.gameObject);
        } else {    
            isDied = true;
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Presumably you started by reviewing past questions in the snake tag, and questions about making Snake in Unity in particular? What did you try based on your research, and how are your results so far different from what you want? \$\endgroup\$ – DMGregory Jan 7 '19 at 12:02
  • \$\begingroup\$ All examples I saw make snake from identical units (squares or circles) if I moved it rotation of units is not important ,in this case rotation and direction is important to move head and tail extensions (body). I tried to make rotation and direction at the same if statement but this make wrong direction so I made rotation line comment. \$\endgroup\$ – Sh.Adel Jan 7 '19 at 12:18
  • \$\begingroup\$ This question is too high-level. What you should do is pick a method, or learn the methods. Parent transforms, 3D rotations, animations, trig functions that might help, and LERPing, then pick the best one that suits you \$\endgroup\$ – Stephen J Jan 7 '19 at 23:45
1
\$\begingroup\$

enter image description here I can't really help with the body, since I've never played snake before, but in regard to your head rotation, the below code should help. Note that I flipped the sprite horizontally in Photoshop, because if his default direction is moving right, then he should also be facing right.

void Update()
{
    if (!isDied)
    {
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.rotation = Quaternion.Euler(Vector3.zero);
        }
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.rotation = Quaternion.Euler(Vector3.forward * -90);
        }
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.rotation = Quaternion.Euler(Vector3.forward * 180);
        }
        else if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.rotation = Quaternion.Euler(Vector3.forward * 90);
        }
    }
}

Basically, you don't need to keep resetting the 'dir'. All you have to do is set it once (Vector2.right) so it'll move, and then you just rotate it while it's moving.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ "As for the body, all you have to do is make it a child of the head. That way, whenever the head moves or rotates, the body will do so as well." This isn't the way the body normally behaves in a snake game . Usually we expect body segments to follow the path traced by the head - so if the head navigates in a square shape, the tail coils around that square, rather than remaining stretched out in a straight line behind and swinging with the head like a rigid board. \$\endgroup\$ – DMGregory Jan 8 '19 at 3:28
  • \$\begingroup\$ You're right. I've never played the game snake before. I've edited my answer to reflect that. \$\endgroup\$ – Sean Carey Jan 8 '19 at 4:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.