Make the Player unable to move to opposite direction
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);
}
}
Answer by Sgt_Spike · Sep 01, 2019 at 09:49 PM
Hey, try using a string for this instead of multiple booleans. This code should work.
public int snakeSpeed = 100;
private string currentDir;
void Start()
{
currentDir = "u";
StartCoroutine(StartGame());
}
IEnumerator StartGame()
{
yield return new WaitForSeconds(3);
InvokeRepeating("Going", 2, 1);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
if (currentDir != "l")
{
Invoke("Right", 1);
currentDir = "r";
}
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
if(currentDir != "r")
{
Invoke("Left", 1);
currentDir = "l";
}
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
if(currentDir != "u")
{
Invoke("Down", 1);
currentDir = "d";
}
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (currentDir != "d")
{
Invoke("Up", 1);
currentDir = "u";
}
}
}
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);
}
The new string will detect which way the player last moved, either u, d, r, or l. When the player moves in a direction, the new if statement will use the string variable to make sure it's not the opposite direction, and if it is the opposite direction, the code will simply not execute. Hopefully it works!
(Also just as a friendly tip, try to name variables with a lowercase letter first, and every other new word for the variable will start with a capital letter, like I have done in the example code. It just makes them easier to read and stops you getting them mixed up with functions.)
Thank you really much! It worked flawlessly! I'd give you reward points but i don't have any unfortunatly. I actually didn't think about the string mechanic, cool move ;)
No worries, glad it helped. And strings can often be replaced with booleans when you have more than two choices. It just saves space rather than using like four different booleans. :D
Your answer
Follow this Question
Related Questions
Player keeps spinning after collision 0 Answers
Help changing input to axis 0 Answers
Character Rotation 0 Answers
Continous movement issue 0 Answers
How can I state the movement speed of this script? 0 Answers