• 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 gabgab06 · Jul 04, 2021 at 04:15 AM · pathalgorithmgrid based game

C# How to get all the tile in a straight path in a grid based game?

Hi,

So I want to do a function that return a list of tile from a grid in a straight path (and each tile shall touch each other) from two coordinate in a grid, the start and the end tile.

I've seen a the Bresenham's Line Algorithm but I just can't understand it since all the video that I see are not explaining that well or I'm dumb.

I could do it with the A* algorithm that I already implemented, but I feel like it would not make perfect line since he just need to find the fastest path and it might be to only go on x until its at the good coordinate and the go on y. (My A* don t work with the diagonal neighbour because I don't want the caracter to go diagonaly(I could make it diagonaly but that would not really solve my problem since I want to get all the tile in a straight and the tile shall touch each other))


the only solutions I thought could go well would be to do a ray cast all and I transform all there position into there tile linked to it, but I feel like it would use a lot of performance compare to the a simple mathmatical formula.


Thanks for the help!alt text

capture.png (266.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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by gabgab06 · Jul 22, 2021 at 03:35 AM

Ok so I tried to make it that they all touch but it's kinda complicated to accomplish but I still got this code that work perfectly :

 List<Cell> validCells = new List<Cell>();
 
         Vector2Int currentPosition = startCell.Position;
         Vector2Int endPosition = endCell.Position;
 
         Vector2Int startDifference = new Vector2Int(Mathf.Abs(endPosition.x - currentPosition.x), Mathf.Abs(endPosition.y - currentPosition.y));
 
         int error = 0;
 
         Vector2Int difference = startDifference * 2;
 
         Vector2Int addition = new Vector2Int(1, 1);
 
         if (currentPosition.x > endPosition.x) addition.x = -1;
         if (currentPosition.y > endPosition.y) addition.y = -1;
 
         if (startDifference.x > startDifference.y)
         {
             error = startDifference.x;
             for (int i = 0; i <= startDifference.x; i++)
             {
                 validCells.Add(_mapManager.GetCell(currentPosition));
                 currentPosition.x += addition.x;
                 error -= difference.y;
                 if (error <= 0)
                 {
                     currentPosition.y += addition.y;
                     error += difference.x;
                 }
             }
         }
         else
         {
             error = startDifference.y;
             for (int i = 0; i <= startDifference.y; i++)
             {
                 validCells.Add(_mapManager.GetCell(currentPosition));
                 currentPosition.y += addition.y;
                 error -= difference.x;
                 if (error <= 0)
                 {
                     currentPosition.x += addition.x;
                     error += difference.y;
                 }
             }
         }

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

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

164 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 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

How to find shortest path between buttons? 1 Answer

restricting player movement with a path? 1 Answer

How to load textures from a local folder on Android 1 Answer

Object movement algorithm 0 Answers

How to steer character by path using Root Motion? 0 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