• Advertisement
Sign in to follow this  

Unity Need help with pathfinding C#

This topic is 843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, im making pathfinding for c# and understand the algorithm but the code is confusing.  How would i be able to devide the amount of set space and make rectangles using graphics that are 50 , 50 pixels and fill the area if the area is a multiple of 50? Im not using XNA or Unity.

Share this post


Link to post
Share on other sites
Advertisement

It is hard to determine what you mean in the original post

 

Hey, im making pathfinding for c# and understand the algorithm but the code is confusing.  How would i be able to devide the amount of set space and make rectangles using graphics that are 50 , 50 pixels and fill the area if the area is a multiple of 50? Im not using XNA or Unity.

 

Do you mean you are doing a 2D game which is on a grid or that is 50*50 pixels in size? Or is this a 3D render and you want to overlay a grid on top that you will path find over?

 

If your game is 2D grid based they you should not worry about the render size of each tile at all, just the coordinates, as the render size of the tile is UI based and all the path finder should care about is the tile you are currently evaluating etc.

 

My Path finder code has no concept about the map/game world or whatever it is navigating over, instead it take the follow arguments, The bounding size of the map, so X,Y,W,H along with a Func<Vector, bool> which the path finder uses to decide if the vector location is a valid move. The caller supplies the function and this is what decouples the route finder from the map. If you can think about it this way it will help split what is A* code away from map and render based stuff which just don't matter :)

 

 

Also preferably a list of the rectangles name in listing them like "myTile1" "myTile2" "myTile3" ect.

 

If you have a grid why would you want to "name" of the tile instead of its coordinates? The only logical reason for the names would be you have the tiles in a map by name and you want to use that for lookup, this is unwise as the cost of string based lookups with a large grid could get costly for something like path finding.

Share this post


Link to post
Share on other sites

Generally the navigation mesh (3D) or navigation grid (2D) is created along with the art. 

 

Often it will encode additional information, such as the type of motion to use on the area (walk, swim, climb, crawl, slippery, etc) and sometimes different areas can be traversed at different times (can swim after getting flippers, can walk on quicksand after getting special boots, etc).

 

WozNZ basically covered the rest.

Share this post


Link to post
Share on other sites

Guys, All i need is a algorithm to make 50, 50 width and height rectangles that fit into a 1000 , 1000 pixel form. i want to name the tiles so that in different locations it could set a boolean for the tile to be unable to pass or not. It is a 2d game. No xna or Unity. all i want is to make multiple rectangles like new Rectangle blah (blah blah) exept without me having to do it a bunch of times.

Share this post


Link to post
Share on other sites

Please be more descriptive. Im trying to make it so there is 44 tiles (i did the math) each 50 by 50 pixels. I want them all named from 1 to 44 using possibly a  loop. Im not sure how loops (or any commands beside manually naming it like (new Rectange recty;)) can be used to name things but if you can find that out it will be extremely helpful.

Share this post


Link to post
Share on other sites

Not sure where you're getting 44 tiles from.... a 1000X1000 pixel area would hold 400 50X50 pixel rects

 

If you're asking to name the variable itself myTile1, myTile2 etc, like:

 

var myTile1 = new Rectangle(...);

 

Then no this is not possible in any reasonable way and is really a horrible idea. Instead, you should have something like a 2d array of Rectangles accessed by their grid location:

 

var width = worldSizeX / tileSizeX;

var height = worldSizeY / tileSizeY;

 

var grid = new Rectangle[width, height]();

 

And then use something like this to build the array:

 

for (var j = 0; j < height; j++)

{

  for (var i = 0; i < width; i++)

  {

    grid[i,j] = new Rectangle(i * tileSizeX, j * tileSizeY, tileSizeX, tileSizeY);

  }

}

Edited by jdean300

Share this post


Link to post
Share on other sites

Im not sure how loops (or any commands beside manually naming it like (new Rectange recty;)) can be used to name things but if you can find that out it will be extremely helpful.


I'm not sure why you're jumping in to pathfinding already without having the fundamentals down already. You MUST understand the basic principles of programming, or else you'll ask extremely weird questions like this.

You can't make a loop to generate multiple *variables* such as "Rectangle myRect1; Rectangle myRect2;" without using code generation, and you should NEVER use code generation for this kind of problem. You can't do ANYTHING to make new variables in C# at runtime unless you're compiling code on the fly, which you shouldn't do. You can only set the values of existing variables and use collections such as arrays, lists, etc.

In this case you need to use an array. Here is an example of a 2D array of Rectangles in a C# WinForms app:
 
Rectangle[,] grid = new Rectangle[20,20];

for (int y=0; y<20; ++y)
{
    for (int x=0; x<20; ++x)
    {
        grid[x,y] = new Rectangle(x * 50, y * 50, 50, 50);
    }
}
And like jdean300 said, where did you get 44 tiles? That isn't even a square number. (1000 / 50) per axis is 20, squared is 400. Edited by Nypyren

Share this post


Link to post
Share on other sites

For the OP, you are trying to connect the appearance of the tile to deep into your code.

 

public class Tile

{

    public string Name { get; set; }

    // some other properties in you tile here

}

 

Now you can store your map as follows

 

var Map = new Tile[20, 20];

 

And loop through the grid adding the tiles using whatever map builder you want.

 

NOTE [20, 20] is based in 1000*1000 pixels divided by your 50*50 tiles

 

Notice how there is no notion of the size of the tiles above, that is all decoupled and is the responsibility of the render engine which could use Tile.Name to determine the graphic representation for the tile.  This way the tiles could be 10*10 or 200*200 on screen or even ACSII text display, it does not matter. 

You have to decouple in this way, think what happens if your game is played on a 4K monitor, your 1000*1000 pixel map is now tiny. The pixel size of tiles should never impact the game engine logic or you will find yourself in a world of pain later down the line. The reality is that if you have grid based movement in your game you should be able to write the engine to control the game that is self contained and has no concept about how input is received or how the game is displayed, they just do not matter to the game logic

 

You need to step back and think about what you are trying to do and possibly take some time out and learn some more about code, abstraction and separation of interests before you revisit your game code.  

Edited by WozNZ

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By RoKabium Games
      Another one of our new UI for #screenshotsaturday. This is the inventory screen for showing what animal fossils you have collected so far. #gamedev #indiedev #sama
    • By eldwin11929
      We're looking for programmers for our project.
      Our project is being made in Unity
      Requirements:
      -Skills in Unity
      -C#
      -Javascript
      -Node.js
      We're looking for programmers who can perform a variety of functions on our project.
      Project is a top-down hack-and-slash pvp dungeon-crawler like game. Game is entirely multiplayer based, using randomized dungeons, and a unique combat system with emphasis on gameplay.
      We have a GDD to work off of, and a Lead Programmer you would work under.
      Assignments may include:
      -Creating new scripts of varying degrees specific to the project (mostly server-side, but sometimes client-side)
      -Assembling already created monsters/characters with existing or non-existing code.
      -Creating VFX
      -Assembling already created environment models
      If interested, please contact: eldwin11929@yahoo.com
      This project is unpaid, but with royalties.
       
      ---
      Additional Project Info:
      Summary:
      Bassetune Reapers is a Player-verus-Player, competitive dungeon crawler. This basically takes on aspects of dungeon crawling, but with a more aggressive setting. Players will have the option to play as the "dungeon-crawlers" (called the 'Knights', or "Knight Class", in-game) or as the "dungeon" itself (literally called the 'Bosses', or "Boss Class", in-game). What this means is that players can choose to play as the people invading the dungeon, or as the dungeon-holders themselves.
      Key Features:
      -Intense, fast-paced combat
      -Multiple skills, weapons, and ways to play the game
      -Tons of different Bosses, Minibosses, creatures and traps to utilize throughout the dungeon
      -Multiple unique environments
      -Interesting, detailed lore behind both the game and world
      -Intricate RPG system
      -Ladder and ranking system
      -Lots of customization for both classes s of customization for both classes
    • By RoKabium Games
      Custom coffee mugs have arrived... More caffeine!
      Have a great weekend everyone! 
      #gamedev #indiedev #sama #caffeine
    • By Atwo Studios
       
      Hey guys,

      Anthony here from Atwo Studios bringing you some new updates for the new year!
      In this video I go over our game ROY, the new games and some general updates to the company!

      If you have not checked out ROY feel free to give it a try! Many people have said they enjoyed the game thus far!
      ROY: https://goo.gl/o6JJ5P
       
    • By Affgoo
      https://play.google.com/store/apps/details?id=com.NE.Alien
      still a lot of work to do, but its pretty stable  please let me know what you think <3
      Atlas Sentry is a game of destroy everything. Using your turret, simply swivel and shoot your way to victory, upgrading your weapons to unleash destruction on the variety of spaceships. The bigger your combo’s the more score you get! Earn silver as you play and then purchase new weapons and abilities to better deal with your enemy. Different enemies use different tactics and weapons, work out your own priorities in their destruction order. 

      Features: 
      **2 different game modes 
      **A level select mode with 20 difficult levels including a final boss, can you defeat it? **Arcade mode of endless destruction, how long will you last? 
      **High scores to compete against others, see who can take the top spot. 
       
  • Advertisement