- Home /
 
 
               Question by 
               Pathke · Aug 02, 2016 at 12:08 PM · 
                2dpathfindingturn-basedstrategychess  
              
 
              Help with my 2d pathfinding
Hi, I am making a chess influenced game & i'm running into a spot of bother, I have generated tiles as game objects in another script & can move my character to whichever tile I please.
I am trying to limit the player to only moving to an adjacent tile either left, right, up, down or diagonally.
But I've spent days of my spare time and cannot find any sort of solution. I've searched online & watched many tutorials so this is my last resort.
Any ideas? Thanks
This is the code I use to move character
 public class ControllerScript : MonoBehaviour {
     //The selected player
     public GameObject selectedPlayer;
     //The selected tile
     public GameObject selectedTile;
     //Target tile vector
     Vector3 target;
     //Press Enter to move text
     public Text confirmText;
     //can the player move
     bool canMove;
     //Movement speed
     int speed;
     //If the character is currently moving
     public bool moving;
     // Use this for initialization
     void Start ()
     {
         //Speed is 4
         speed = 4;
         //cant move at start of game
         canMove = false;
         //is not moving at start of game
         moving = false;
     }
     
     // Update is called once per frame
     void Update ()
     {
         //If there is both a selected player and tile
         if (selectedPlayer != null && selectedTile != null)
         {
             //The player can move
             canMove = true;
             //Call MovePlayers function
             MovePlayers();
         }
         //Else,
         else
             //The player cannot move
             canMove = false;
         //If right click and player is not moving
         if(Input.GetKeyDown(KeyCode.Mouse1) && moving != true)
             //call right click function
             RightClick();
         //If the player can move
         if(canMove == true)
             //Show the text to confirm move with enter key press
             confirmText.transform.localScale = new Vector3(1, 1, 1);
         //Else, hide the text
         else
             confirmText.transform.localScale = new Vector3(0, 0, 0);
     }
     //Called on right click with no movement ongoing
     void RightClick()
     {
         //IF there is no selected tile
         if (selectedTile == null)
             //deselect the player
             selectedPlayer = null;
         //Deselect the tile
         selectedTile = null;
     }
     //Called when there is a selected player & tile
     void MovePlayers ()
     {
         //On enter press
         if (Input.GetKeyDown(KeyCode.Return))
             //Movement is now happening
             moving = true;
         //If player can move
         if (canMove == true)
         {
             //Target vector x & y is equal to selected tile x & y
             target.x = selectedTile.transform.position.x;
             target.y = selectedTile.transform.position.y;
             //If moving
             if (moving == true) 
             {
                 //Move towards the target position, from original position
                 selectedPlayer.transform.position = Vector3.MoveTowards(selectedPlayer.transform.position, target, speed * Time.deltaTime);
             }
         }
         //If at the target
         if (selectedPlayer.transform.position == selectedTile.transform.position)
         {
             //Moving is not true
             moving = false;
             //Deselect tile
             selectedTile = null;
         }
 
     }
 }
 
              
               Comment
              
 
               
              Your answer