Restricting axis of movement not working at all!

Hey people. I’m doing a board game on which the player (cube) can only move in directions where there is no obstacle. Kind of like pudding monsters game.

THE EXACT PROBLEM WITH THE CURRENT METHOD:

  1. Lets say I am on the right corner of the board, and I move forward. Now, I am still on the right edge of the board, and there is an obstacle to my front. No of moves = 1. Now, I hit the right button (even though Im already on the edge and cant move to the right ideally), the player moves a tiny amount to the right, and the moves count increases. The next time I hit right, there is no issue. But NOW if I hit front, it will move a little to the front (almost invisible) but the moves count increases.
    What SHOULD happen is that on hitting right OR front, the moves count should NOT increase, and there should be no little movements.

What I’m implementing now is, 3 functions called in the update:
drawrays() - this function is used to draw rays from all 4 sides of the player (cube). It calculates the distance between player and the object in each direction.

getInput() - this function gets the input either w,a,s, or d. Depending on the input, it moves in a certain direction.

DoMovement() - 4 if statements, for 4 movement directions. Depending on the variable, corresponding movement is done (via itween). Here, say if you put W (for forward), the boolean forward movement restriction gets true, rest are false.

Here is the exact code, which will help me explain better!

using UnityEngine;
using System.Collections;

public class move7 : MonoBehaviour {
	
	GameObject player, prevpos, tile;
	bool forward = false, backward = false, left = false, right = false;
	bool moving = false;
	bool restrictF = false, restrictL = false, restrictB = false, restrictR = false;
	RaycastHit lefthitter, righthitter, fronthitter, backhitter, tilechecker;
	float L,R,F,B, movetime=0.5f;
	int moves=0, points=0, score=0;
	bool collectedbonus = false, levelcomplete = false;
	static int currlevel=0;
	Vector3 savedpos = new Vector3 (0,0,0);
	Transform tile2;
	public Texture floormaterial;
	int nooflevels = 6;
	
/*---------------------------------------------------------------------------------------------------------------*/
	
	// Use this for initialization
	void Start () {
		player = GameObject.FindGameObjectWithTag("Player");
	}
	
/*---------------------------------------------------------------------------------------------------------------*/
	
	// Update is called once per frame
	void FixedUpdate () 
{
		drawrays ();	
		getInput(); 
} //END OF UPDATE!
	

/*---------------------------------------------------------------------------------------------------------------*/
	
	
/*---------------------------------------------------------------------------------------------------------------*/
	
	void getInput()
	{
		if(Input.GetKey(KeyCode.W) && !restrictF)
		{
			forward = true;
			backward = false;
			left = false;
			right = false;
			moves++;
			restrictF = true;
			restrictL = true;
			restrictB = true;
			restrictR = true;
		}
		
		else if(Input.GetKey(KeyCode.A) && !restrictL)
		{
			left = true;
			forward = false;
			backward = false;
			right = false;
			moves++;
			restrictF = true;
			restrictB = true;
			restrictL = true;
			restrictR = true;
			
		}
		
		else if(Input.GetKey(KeyCode.S) && !restrictB){
			backward = true;
			forward = false;
			left = false;
			right = false;
			moves++;
			restrictF = true;
			restrictB = true;
			restrictL = true;
			restrictR = true;
			
			
		}
		
		else if(Input.GetKey(KeyCode.D) && !restrictR)
		{		
			right = true;
			forward = false;
			backward = false;
			left = false;
			moves++;
			restrictF = true;
			restrictB = true;
			restrictR = true;
			restrictL = true;
			
			
		}		
		
		doMovement(); //actually moves the playe
		
	}
		
/*---------------------------------------------------------------------------------------------------------------*/
	
	void doMovement()
	{
		
		if(forward)
		{
			iTween.MoveAdd(player, iTween.Hash("z", F-0.5f, "time", movetime));
			restrictF=true;
			restrictL = false;
			restrictB = false;
			restrictR = false;
			forward=false;
			//print ("Allow L,R,F,B:" + restrictL + restrictR + restrictF + restrictB);
			
		}
		
		else if(backward)
		{
			//player.transform.Translate(0,0,(-B+0.5f));
			iTween.MoveAdd(player, iTween.Hash("z", -B+0.5f, "time", movetime));
			restrictB=true;
			restrictL = false;
			restrictF = false;
			restrictR = false;
			backward=false;
			//print ("Restrict L,R,F,B:" + restrictL + restrictR + restrictF + restrictB);
			
		}
		
		else if(left)
		{
			//player.transform.Translate(-L+0.5f,0,0);
			iTween.MoveAdd(player, iTween.Hash("x", -L+0.5f, "time", movetime));
			restrictL=true;
			restrictF = false;
			restrictB = false;
			restrictR = false;
			//print ("Allow L,R,F,B:" + restrictL + restrictR + restrictF + restrictB);
			
		}
		
		else if(right)
		{
			//player.transform.Translate(R-0.5f,0,0);
			iTween.MoveAdd(player, iTween.Hash("x", R-0.5f, "time", movetime));
			restrictR=true;
			restrictL = false;
			restrictB = false;
			restrictF = false;
			//print ("Allow L,R,F,B:" + restrictL + restrictR + restrictF + restrictB);
		}
		
	}
	
/*---------------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------------*/
	
	void drawrays()
	{
					
		if(Physics.Raycast(transform.position,Vector3.forward,out fronthitter))
	 	{
			F=fronthitter.distance;
	 	}//end of raycast checking forward wall
		if(Physics.Raycast(transform.position,Vector3.left,out lefthitter))
	 	{
	 		
			L=lefthitter.distance;
	 	}
		if(Physics.Raycast(transform.position,Vector3.right,out righthitter))
	 	{
			
			R=righthitter.distance;
	 	}
		if(Physics.Raycast(transform.position,-Vector3.forward,out backhitter))
	 	{
				
			B=backhitter.distance;
	 	}
		
	}		
	
}//END OF CODE

Please let me know if it’s still not clear. I will try to explain more thoroughly.

Please do help, my other questions are still unanswered as well, if you could help me out there too I’d appreciate it :slight_smile:

I do not know but I am gettin similar issues. It is really irritating. I think the issue is with the values of the booleans u have set.