- Home /
Prevent player from moving in a certain direction while moving in another
Hi. I am making a Tron light-cycle game and I having some trouble figuring how to stop the player from moving on the opossite direction that he is already moving. Right now I am creating a colored square on each change of direction and between them I create the light wall, the problem occurs when I am moving in a single direction since I just have one colored square of the two I need to create a solid wall with fitColliderBetween, the light wall behind the player is not solid yet and the player can move freely up and down the wall until he hits the last colored square used to initiate the fitColliderBetween or until he moves again in the direction he was facing initially and hit the square created when he turned in the opossite direction that he was moving. This can be prevented by disabling the input for moving in the undesired direction. If the player is moving UP, he should not move DOWN but he can choose to move RIGHT or LEFT.
If there a way to disable the player input for a single direction? In this case the opposite one. I already have a way of telling which way the player is facing (used to spawn projectiles) but I do not know how to disable a single input, I found similar answers but they wanted to disable all player input while keeping the player's momentum and I just want to disable only one direction.
public enum PlayerFacing
{UP, DOWN, LEFT, RIGHT}
public PlayerFacing facing;
// Use this for initialization
void Start () {
//Initial velocity so it starts moving
GetComponent<Rigidbody2D> ().velocity = Vector2.up * speed;
spawnWall ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (upKey)) {
// Do is key pressed
GetComponent<Rigidbody2D> ().velocity = Vector2.up * speed;
//Spawn wall
spawnWall ();
facing = PlayerFacing.UP;
} else if (Input.GetKeyDown (downKey)) {
GetComponent<Rigidbody2D> ().velocity = -Vector2.up * speed;
spawnWall ();
facing = PlayerFacing.DOWN;
} else if (Input.GetKeyDown(rightKey)) {
GetComponent<Rigidbody2D> ().velocity = Vector2.right * speed;
spawnWall ();
facing = PlayerFacing.RIGHT;
} else if (Input.GetKeyDown (leftKey)) {
GetComponent<Rigidbody2D> ().velocity = -Vector2.right * speed;
spawnWall ();
facing = PlayerFacing.LEFT;
}else if (Input.GetKeyDown (shotKey) && Time.time > nextFire) {
nextFire = Time.time + fireRate;
//Instantiate (basicshot, bulletSpawn.position, Quaternion.identity);
BasicShoot (basicshot, 150);
}
// Fill between walls
fitColliderBetween (wall, lastWallEnd, transform.position);
}
Answer by PouletFrit · Jul 21, 2015 at 04:45 PM
Just add a variable to keep track of your direction and add a condition in your input verification
// Create an enum and simply cache your direction in a variable
public enum Direction {
Left, Right, Up, Down
}
public Direction currentDirection;
// And then update currentDirection appropriatly in update and check it
if (Input.GetKeyDown(upKey) && currentDirection != Direction.Down) {
// your movement code here
currentDirection = Direction.Up;
} else if ...
also i suggest you to cache your rigidbody2D in a variable instead of refetching it in every update.
Thanks a lot. It works perfectly. I was so focused on disabling input that I could not come with any other way of doing it.
Since the player is always moving, PlayerFacing already has the current direction so it is easy to add the extra code. Thanks a lot.
Your answer
Follow this Question
Related Questions
Prevent Player from moving into opposite direction than he already is 1 Answer
My controls are inverted when facing sideways. 1 Answer
How can I stop the player movement from being too fast while moving diagonally in my 2D game? 2 Answers
2D Top-Down Movement: Transform.up and right don't change 2 Answers