• 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 j4rey14 · Aug 08, 2016 at 02:50 PM · 2d gamepathfindingtiled

How to implement pathfinding with Tiled2Unity prefabs?

I have implemented A* pathfinding algorithm using 2D array in C#.

However when I import Maps from Tiled2Unity, it is imported using a mesh and I can't iterate from tile to tile. How do I go about implementing pathfinding?

Comment
Add comment · Show 2
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 Cherno · Aug 08, 2016 at 08:57 PM 0
Share

You haven't stated what kind of pathfinding solution you use.

Generally, you could just assign the mesh to a navmesh graph, or go through the points of a polygon collider and generate the nodes from there.

avatar image j4rey14 Cherno · Aug 09, 2016 at 06:13 AM 0
Share

I have studied and written an A* pathfinding algorithm using 2D array in C# which is working, so I have an understanding of how it works.

I was expecting to use that algorithm in Unity 2D tile game, but as I imported the map from Tiled2Unity. I find that there is no tile to iterate through.

I have created a map in Tiled and added the collision boxes too, after importing the map Unity, the map with the collision box are working when I use the key input to move the character.

Could you please provide some tutorial/example link.

PS: I am new to Unity.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by senad · Aug 09, 2016 at 09:22 AM

For a project I once implemented 2D grid-based pathfinding on a 3D map. So I projected the grid onto the map via orthographic projection. Then I just manually calculated the x/y positions in the center of each cell.

In another step I iterated through each cell and did a raycast to find out the height for each cell. I then used this as a simple navigation graph. The height differences between cells were small, so it did not produce any optical glitches and worked very well.

Is this similar to what you try to achieve?

Comment
Add comment · Show 2 · 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 j4rey14 · Aug 13, 2016 at 07:43 AM 0
Share

That was what I thought I would do, a 2D grid array would hold the map and I would iterate through the array to find the path. However when I imported the $$anonymous$$ap from Tiled, it was a mesh and I don't have a 2D array.

I did some googling and I found Nav$$anonymous$$esh which might work, but it doesn't do tiled navigation.

avatar image Cherno j4rey14 · Aug 13, 2016 at 08:37 AM 0
Share

Navmesh would work, but only for top-down games. The path nodes wouldn't sit in the center of each tile, but in the center of each triangles of which there are two for each tile. If characters can roam freely ins$$anonymous$$d of being restricted to the tiles, then it's ok. If not, then you would need to implement your own custom import function for Tiled2Unity. You could also a hacky workaround like this: Go through each vertex group (3 per triangle) of the mesh by looping through it's triangles array. Compare their x positions; two should be the same while one should be smaller or bigger. This is the first hard angle of the triangle. Then compare this vertex's y position with the other two's. The one that doesn't have the same y position is the other hard angle. The coordinate between these two is the center of the quad. Of course, since each tile/quad has two triangles, you'd still have to find a way to avoid dublicates.

Check out this excellent tutorial:

The Real-Life Adventures of $$anonymous$$ega Dad with Tiled2Unity

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Patfinding cant change destination Target 0 Answers

A* Path Finding (AI Destination Setter) Trying to set Target Transform to Nearest tagged Game Object (2D), 0 Answers

Add 2D Lighting to Scene based on Tiled 0 Answers

Having trouble steering gameobject. 0 Answers

Path Finding using 2 dimensional array 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges