• 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 /
  • Help Room /
avatar image
0
Question by EpsilonQoppa · Feb 09, 2017 at 02:12 AM · transformvector3

How To: Calculate Line Of Best Travel For Grid From Line

I'm working on a game where units move on a game board (like a chess board) and I'm having trouble figuring out which blocks to travel on to maintain straight lines of travel on the board.

Meaning some sort of algorithm that can turn a straight line, vector to vector, to multiple vectors based on the direction it has to travel to get to the selected square.

alt text

I could work something up that just determined at what point that a unit needs to turn - this would most likely make them follow the blue path.

What I'm looking for is some sort of formula that could map those vectors along the green line.

boardvectors.png (136.7 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by EpsilonQoppa · May 10, 2017 at 09:02 PM

Ya'll could have just been like, look up Dijkstra's algorithm..

It actually turned out to be quite simple.

 public class PathingHandler 
 {
     public static int ParseVectorDistance(Vector2 delta, Vector2 gamma)
     {
         float xDistance = Mathf.Abs(delta.x - gamma.x);
         float yDistance = Mathf.Abs(delta.y - gamma.y);
 
         int i = (int)(xDistance + yDistance);
 
         return i;
     }
 
     public static GameBoardComponent DetermineNextNode
         (GameBoardComponent current, Vector2 destination)
     {   
         int currentLowestDistance = Int16.MaxValue;
         int currentLowestHeuristic = Int16.MaxValue;
 
         GameBoardComponent closestComponent = null;
         Vector2 origin = current.Vector;
        
         GameBoardComponent[] components = current.GetNeighbors();
 
         for (int i = 0; i < components.Length; i++)
         {
             if (!components[i].IsPassable) 
                 break;
 
             int h = ParseVectorDistance(components[i].Vector, destination);
             int g = ParseVectorDistance(components[i].Vector, origin);
             int d = h + g;
 
             if (d < currentLowestDistance)
             {
                 closestComponent = components[i];
                 currentLowestDistance = d;
                 currentLowestHeuristic = h;
             }
 
             else if (d == currentLowestDistance)
             {
                 if (h < currentLowestHeuristic)
                 {
                     closestComponent = components[i];
                     currentLowestDistance = d;
                     currentLowestHeuristic = h;
                 }
 
                 else if (h == currentLowestHeuristic)
                 {
                     if (Utility.RandomBoolean())
                     {
                         closestComponent = components[i];
                         currentLowestDistance = d;
                         currentLowestHeuristic = h;
                     }
                 }
             }
         }
 
         if (closestComponent != null) return closestComponent;
         else 
             return null;
     }
 }



alt text


path.png (254.0 kB)
Comment
Add comment · Share
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
avatar image
0

Answer by m-coto · May 12, 2017 at 03:47 PM

You need to split the main "Red" vector's components into different vectors. Assuming you board is on the XZ plane at a "Y" height of 0f, you'll need to do something like this to get the vectors you need: For the "Blue" vectors you'll need to split Red into two new Vector3:

   Vector3 blueX;
   Vector3 blueZ;
 
   void Split(Vector3 red)
   {
     blueX = new Vector3(red.x, 0f, 0f);
     blueZ = new Vector3(0f, 0f, red.z);
   }

For the "Green" vectors you'll need to get which one is the largest component, in order to split that component into 2, you'll end up using 2 times the same Vector3. So in order to get to the goal, you'll have to walk along green1 --> green2 --> green1 again:

 Vector3 green1;
 Vector3 green2;
 
   void SplitThreeWay(Vector3 red)
   {
     if (red.x > red.z)
     {
       green1 = new Vector3(red.x / 2f, 0f, 0f);
       green2 = new Vector3(0f, 0f, red.z);
     }
     else
     {
       green1 = new Vector3(0f, 0f, red.z / 2f);
       green2 = new Vector3(red.x, 0f, 0f);
     }


Comment
Add comment · Show 1 · Share
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
avatar image EpsilonQoppa · May 27, 2017 at 03:27 AM 0
Share

I appreciate the help sire, it actually turned out to be quite simple. I'll put the code up top.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving GameObject a specific distance in the Z direction and back again - regardless of rotation 1 Answer

InverseTransformPoint() help 0 Answers

I got Some Error about how to Transform Postiton Enemy where one point can spawn 2 enemies. 1 Answer

Are I stupid? Vector3 1 Answer

MoveTowards instantly repositioning object to target position 2 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