1
\$\begingroup\$

So I'm making a copy of noobtuts tutorial on Snake

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

I have four borders each with a Box Collider 2D, nothing fancy about them, and a Snakehead with a Box Collider 2D, 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 moves 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: enter image description here

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? I've never had to tell an object that when it hits another object to not move through it from what I can remember.

\$\endgroup\$

3 Answers 3

3
\$\begingroup\$

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.

3.check your script:

enter image description here

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")
        {
            Application.LoadLevel(Application.loadedLevel); //RESTART
        }
    }
}

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
        {
            Application.LoadLevel(Application.loadedLevel); //RESTART
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ 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(); \$\endgroup\$
    – Bearzerk
    Jul 6, 2016 at 12:19
0
\$\begingroup\$

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;

private Rigidbody2D snakeHead;

void Start()
{
 snakeHead = GetComponent<Rigidbody2D>();
}

void FixedUpdate()
{
 Move();
}

void Move()
{
 snakeHead.velocity = dir * snakeSpeed;
 //Or
 //snakeHead.velocity = dir * Time.fixedDeltaTime * snakeSpeed; // In this case you have to increase snakeSpeed.
}
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Bearzerk
    Jul 6, 2016 at 12:21
0
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

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