Hello everyone, i am making a game that has a classic “Snake” game style movement, and for this reason, i need to make the player unable to go to the left if he previously went right, same thing for Up/Down.
I tried setting up some booleans but it didn’t work well, so i don’t know how to make this work, if anyone could help me, i’d be really grateful (note: the game is in 3D if is a needed piece of info)
This is my code, don’t get scared by the 70+ lines, i write really clear and simple code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public int SnakeSpeed = 100;
// Start is called before the first frame update
void Start()
{
StartCoroutine(StartGame());
}
IEnumerator StartGame()
{
yield return new WaitForSeconds(3);
InvokeRepeating("Going", 2, 1);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
Invoke("Right",1);
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Invoke("Left", 1);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
Invoke("Down", 1);
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Invoke("Up", 1);
}
}
void Going()
{
transform.Translate(0, 0, SnakeSpeed * Time.deltaTime);
}
void Right()
{
transform.localEulerAngles = new Vector3(0, 90,0);
}
void Left()
{
transform.localEulerAngles = new Vector3(0, -90, 0);
}
void Up()
{
transform.localEulerAngles = new Vector3(0, 0, 0);
}
void Down()
{
transform.localEulerAngles = new Vector3(0, -180, 0);
}
}