I am learning to create a snake game (from this tutorial) .

The problem is arising when the object(Snake) collides with the borders,nothing happens and it moves out of the box (white square on the left).

I have added BoxCollider2D components on the bounds.

enter image description here

For the player (Snake), I've added BoxCollider2D component and Rigidbody2D.

Here's the script attached to the snake

using UnityEngine;
using System.Collections;

public class Snake : MonoBehaviour {

    Vector2 dir=Vector2.left;

    // Use this for initialization
    void Start () {
        InvokeRepeating ("Move", 0.3f, 0.3f);


    // Update is called once per frame
    void Update () {


    void Move(){
        transform.Translate (dir);

1 Answer 1


Assuming you are following the tutorial, to collision detection does not occur for the snake through Unity's normal physics engine since those options are turned off. Instead, what you do in the tutorial (and in many cases outside of it), is use the OnTriggerEnter2D function to react to the collision.

In that function, you check for collisions with the food objects and the bounding boxes. In the event you are colliding with a bounding box, you program the "lose" function in your game. An example of that would be (in pseudocode):

if collide with food:

The tutorial leaves it up to the reader to create the lose game function.


You must log in to answer this question.

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