The head of the snake has a rigidbody2D and a trigger collider, but nothing happens when the snake collides the wall or its tail.The snake moves by InvokeRepeating a function that changes the position of the snake game object . This kind of problem has never happened before on other projects, does that has something to do with the snake movement?
The script of the boundary:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "player")
{
Debug.Log("entered");
Destroy(other.gameObject);
}
}
check the isTrigger in collider
Here is the movement script
public class Movement : MonoBehaviour
{
public GameObject player;
[HideInInspector] public Vector2 playerPos;
public GameObject trail;
public int direction;
public Vector2 rotation = new Vector2(0, 0);
public float unit;
void Start()
{
playerPos = player.transform.position;
InvokeRepeating("Move", 0, 0.055f);
}
void Update()
{
if (direction != 2 && Input.GetKeyDown(KeyCode.W))
{
direction = 1;
}
if (direction != 4 && Input.GetKeyDown(KeyCode.A))
{
direction = 3;
}
if (direction != 3 && Input.GetKeyDown(KeyCode.D))
{
direction = 4;
}
if (direction != 1 && Input.GetKeyDown(KeyCode.S))
{
direction = 2;
}
}
void Move()
{
switch (direction)
{
case 1:
playerPos = new Vector2(playerPos.x, playerPos.y + unit);
player.transform.position = playerPos;
Instantiate(trail, new Vector2(playerPos.x, playerPos.y - 1), Quaternion.identity);
break;
case 2:
playerPos = new Vector2(playerPos.x, playerPos.y - unit);
player.transform.position = playerPos;
Instantiate(trail, new Vector2(playerPos.x, playerPos.y + 1), Quaternion.identity);
break;
case 3:
playerPos = new Vector2(playerPos.x - unit, playerPos.y);
player.transform.position = playerPos;
Instantiate(trail, new Vector2(playerPos.x + 1, playerPos.y), Quaternion.identity);
break;
case 4:
playerPos = new Vector2(playerPos.x + unit, playerPos.y);
player.transform.position = playerPos;
Instantiate(trail, new Vector2(playerPos.x - 1, playerPos.y), Quaternion.identity);
break;
}
}
}