BoxCollider2D Not Working

So I'm making a copy of noobtuts tutorial on Snake - http://noobtuts.com/unity/2d-snake-game

I've done all their steps and am at the point of extending the game solo, however I've an issue with the basic set-up. My box colliders aren't working at all!

I've 4 borders each with a Box Collider 2D, nothing fancy about them, then I've a SnakeHead with a Box Collider 2D, also nothing fancy about that either. Only the food prefab I have uses the IsTrigger. The Snake head also has a Rigidbody 2D and I've experimented with putting a Rigidbody2D on the borders too but it's not solved my problem.

Basically when I have the Snakehead move to the border, it just passes through the border! It doesn't stop, it doesn't do anything, just continues straight through the border.

I've a basic gif showing off what I mean - http://imgur.com/sITs7eK.

The only different thing about the box colliders is that for the snake they're 0.7 in size, not 1. But even when put to 1, they don't work properly.

Why isn't it stopping the snake from moving? why can it just pass through for no gorram reason I can think of. I've never had to tell an object that when it hits another object to not move through it from what I can remember.

In unity when collision don't work you can fix the problem by following the below steps:

1.Set Collision and Triggers based on game(If game was 2d you should use BoxCollider2D) and check BoxCollider2D trigger Boolean.

2.you should check Rigidbody Component that attached to objects because Collision need physics component(if game was 2d you should use RigidBody2D) and disable isKinematic because Collision don't work.

4.If you can not fix your problem perhaps wrong settings go Edit/project Setting/Physics or Physics2D

So let's define when snake hit with wall reset game; create 4 Boxcollider2D and attach them Rigidbody2D using UnityEngine; using System.Collections;

public class Snake: MonoBehaviour {

void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Wall")
{
}
}
}


second way:you can use Trigger and define when snake exit from scope of the game Reset game

using UnityEngine;
using System.Collections;

public class Snake: MonoBehaviour {

void OnTriggerExit2D(Collider2D coll)
{
if (coll.gameObject.tag == "Zone")//allowed scope of the game so walls are not included
{
}
}
}

• Thank you, it's now working and hitting my debug.log code, I'm not sure why it was being so fussy earlier but regardless thank you so much. A minor edit however to your answer - application.loadlevel is now obsolete, for anyone reading this, use SceneManger.LoadLevel(); – Bearzerk Jul 6 '16 at 12:19

Use transform.Translate(dir * Time.deltaTime * snakeSpeed); instead of transform.Translate(dir); on Move() method. Keep a float variable called snakeSpeed.

But you should not work with translation of a rigidbody. Use velocity instead.

Code should be like:

[SerializeField]
private float snakeSpeed = 10f;

void Start()
{
}

void FixedUpdate()
{
Move();
}

void Move()
{
snakeHead.velocity = dir * snakeSpeed;
//Or
//snakeHead.velocity = dir * Time.fixedDeltaTime * snakeSpeed; // In this case you have to increase snakeSpeed.
}

• Does that however move pixel by pixel? Because that's the look I'm going for and Transform does that very well, if I use velocity I'll just be sliding the object along, not making it jump X pixels per movement. – Bearzerk Jul 6 '16 at 12:21

I had the same issue when i was making a breakout game it seems that Kinematic rigidbody collider don't interact with static ones you can check the table here: https://docs.unity3d.com/Manual/CollidersOverview.html.

for me i've overcame the issue by disabling isKinematic and setting the GravityScale to 0. you can either do that or use a script to limit the snake movement by comparing its coordinates with the borders' coordinates.