# How to restrict a direction based on the current direction?

We’re working on a snake game and we don’t want it to die when we head backwards with the joystick.
We’ve tried a lot of ways, inclusing a question already answered on this forum.
Thank you in advance, here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class Snake : MonoBehaviour
{

``````enum Direction
{
up,
down,
left,
right
}

//string lastDir = "a";
//string errOrd = "b";

Direction direction;

public List<Transform> tail = new List<Transform>();

public float frameRate = 0.1f;
public float step = 0.14f;

public GameObject TailPrefab;

public Vector2 horizontalRange;
public Vector2 verticalRange;

/**
public float[] xpositions = {-0.15f, -0.3f, -0.45f, -0.6f, -0.75f, -0.9f, -1.05f, -1.2f, -1.35f, -1.5f, -1.65f, -1.8f, -1.95f, -2.1f, -2.25f, -2.4f, -2.55f, -2.7f, 0.0f, 0.15f, 0.3f, 0.45f, 0.6f, 0.75f, 0.9f, 1.05f, 1.2f, 1.35f, 1.5f, 1.65f, 1.8f, 1.95f, 2.1f, 2.25f, 2.4f, 2.55f, 2.7f};
public float[] ypositions = {-0.15f, -0.3f, -0.45f, -0.6f, -0.75f, -0.9f, -1.05f, -1.2f, -1.35f, 0.0f, 0.15f, 0.3f, 0.45f, 0.6f, 0.75f, 0.9f, 1.05f, 1.2f, 1.35f, 1.5f, 1.65f, 1.8f, 1.95f, 2.1f, 2.25f, 2.4f, 2.55f, 2.7f, 2.85f, 3.0f, 3.15f, 3.3f, 3.45f, 3.6f, 3.75f, 3.9f, 4.05f, 4.2f, 4.35f, 4.5f, 4.65f, 4.8f};
**/

void Start()
{

InvokeRepeating("Move", frameRate, frameRate);

}

void Update()
{
float y = CrossPlatformInputManager.GetAxis("Vertical");
float x = CrossPlatformInputManager.GetAxis("Horizontal");

Vector2 moveVec = new Vector2(x, y);

if (Mathf.Abs(x) < Mathf.Abs(y))
if (y > 0)
direction = Direction.up;
else direction = Direction.down;

else if (Mathf.Abs(x) > Mathf.Abs(y))
if (x < 0)
direction = Direction.left;
else
direction = Direction.right;
}

void Move()
{
lastPos = transform.position;

Vector3 nextPos = Vector3.zero;
if (direction == Direction.up)
nextPos = Vector3.up;
else if (direction == Direction.down)
nextPos = Vector3.down;
else if (direction == Direction.left)
nextPos = Vector3.left;
else if (direction == Direction.right)
nextPos = Vector3.right;

nextPos *= step;
transform.position += nextPos;

MoveTail();
}

Vector3 lastPos;
void MoveTail()
{
for (int i = 0; i < tail.Count; i++)
{
Vector3 temp = tail*.position;*
``````

tail*.position = lastPos;*
lastPos = temp;
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag(“Block”))
{
print(“Game Over”);
}
else if (col.CompareTag(“Coin”))
{
col.transform.position = new Vector2(Random.Range(horizontalRange.x, horizontalRange.y), Random.Range(verticalRange.x, verticalRange.y));
/**
var rng = new System.Random();
float xposition = xpositions[rng.Next(xpositions.Length)];
float yposition = ypositions[rng.Next(ypositions.Length)];
col.transform.position = new Vector2(xposition,yposition);**/
}
}
}

I haven’t tested it, but the first thing that comes to my mind is:

Instead of making an enum for `Direction ` store the direction in the Snake class as a `Vector2 `, instead of `Direction direction; ` use `Vector2 direction; `.

In `Update() ` before making changes to the `Vector2 direction ` field check if `moveVec ` isn’t the direct opposite of the stored direction vector.

``````Vector2 moveVec = new Vector2(x, y);
if (moveVec != -direction)
{
if (Mathf.Abs(x) < Mathf.Abs(y))
...
}
``````

In the `Move() ` method in the if statements switch `Direction.up ` to `Vector2.up `, `Direction.down ` to `Vector2.down `, aso.

That theoretically should prevent the player from changing the move direction to the direct opposite of the current direction vector.