Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

When the development team will implement 2D Navmesh? 0 Answers

2D Pathfinding: how to create navMesh 0 Answers

Astar Pathfinding ai moves more nodes than it should 0 Answers

A* Formations 1 Answer

How do I implement A* pathfinding to my 2d game, without tiles? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges