Search Unity

Requesting help with a 2d Array to define cell properties. passable/not passable

Discussion in 'Scripting' started by Ham08, Apr 23, 2014.

  1. Ham08

    Ham08

    Joined:
    Dec 28, 2013
    Posts:
    15
    Greetings and thank you very much for taking the time to consider my request and much gratitude if you can actually help.

    SCENARIO AND PROBLEM:
    I am using a player controller script that implements step movement in a grid fashion that I want (like Dungeon Master of the old days), but the problem is that it does not include collision detection so the player walks through walls and other what should be impassable terrain. Some of you may be familiar with the Grid Move script located on the wiki: http://wiki.unity3d.com/index.php/GridMove (I am using a modified version by SteveJ posted in the thread below) (My game will have 2d movement rendered in 3d like Legend of Grimrock, etc.)

    Eric5h5 posted in a thread talking about the movement I want and suggested to use a 2d array to represent the world and set the properties of each cell, then query that array to see if movement is possible.

    REQUEST FOR HELP:
    <Humbly Bows Head> Please, I am trying to learn how to program and use a 2d Array to define cell properties on my map in a grid fashion so that I can set passable/not passable terrain. In other words: floor (passable), lava (not passable), wall (not passable), etc. I don't want to draw the terrain with this method (would be invisible), I just want to define the cell properties and then query them in the Grid Move script (as modified by SteveJ) before actual movement to determine if the player can go to that location or not. ( 2 dimensional coordinates to represent each cell and the properties passable/not passable/other.)

    I have searched for months, but it doesn't look like the solution to this particular problem was ever revealed on the forums here. I haven't had any luck with Google search either.

    My request is not just what to do, but how to do it, as well.

    SPECIFIC:
    I need a tiny example that shows the entire process using Unity 3d and scripting in either JS/US or CS. Either would be fine. A tiny dungeon with small rooms and some hallways would work (would have walls to block movement and floors to walk on, etc.) I should be able to take the explanation and expand it to create my own complete dungeon levels/over-world levels guided by the 2d array to govern which cells are passable/not passable.

    I don't know how large an example 2d array script this would be or how long it would take to thoroughly explain it. I just hope it is not too much trouble for you to explain in detail for a newbie that really wants to learn exactly how to do it.

    If there are any Unity Engine preference/configurations that I need to alter, please clarify. Please do not leave any steps out. Thank you.

    I am grateful for your valued time, consideration, and effort! Seriously! This is a big request and I feel bad that I even have to ask for help.

    Cheers!
     
    Last edited: Apr 23, 2014
  2. Synapse11

    Synapse11

    Joined:
    Apr 18, 2014
    Posts:
    39
    I'm not a programmer, but each cell I imagine would have a boolean:

    North Possible Y/N
    East Possible Y/N
    West Possible Y/N
    South Possible Y/N

    If possible is N, you can turn in that direction but if W is pressed return some kind of invalid noise and don't process the movement.
    Hopefully someone can put this concept into a nice piece of code for you.
     
  3. Ham08

    Ham08

    Joined:
    Dec 28, 2013
    Posts:
    15
    I'm not a programmer, either. The logic makes perfect sense, but I just do not know how to do it in code. Though, I believe I am smart enough to take a complete and thorough example 2d array using the Grid Move script (as modified by SteveJ) and expand upon it for my entire project.

    I know it is not true of most programmers, but it sure seems like many have little to no desire to help non-programmers since they don't gain anything from it (ie., I would never be able to return the favor) and my words of gratitude do nothing to make their effort worth while. To be frank, I don't blame them.
     
  4. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    That tiny example is a whole game onto itself, and is the equivalent of building a whole Pac-Man level. So there's nothing tiny about it. It would have to be one very generous individual who would be willing to spend the time to make that example and expect nothing in return.
     
  5. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    As long as you can access each point in the array and tell if it's passable or impassable, you just test the +/- of your two axes relevant to the player and set four booleans allowing you to use only the possible directions' keys. The coding for something like that is actually really simple, if you want to get into coding, it's probably a good starting project.
     
  6. Ham08

    Ham08

    Joined:
    Dec 28, 2013
    Posts:
    15
    I am not asking for an entire level. I am asking for an example to show how it works. It could be as simple as a hallway and a small room with walls. I need the 2d array to show an example of how it, working with the Grid Move script (as modified by SteveJ) as the player controller, would prevent a player from walking through walls and other impassable terrain.

    How is that done, exactly? You say that this is a really simple thing to do, yet it appears as though it is not simple enough to just go ahead and explain it and provide an example 2d array script working with the Grid Move Script (as modified by SteveJ). How long would it take to explain it? An hour? Several hours? Then it's not that simple, is it? A few minutes? Then why will no one help if it is such an easy thing to do? Would that not make the community a better place overall? One where everyone helps each other when they can, when it's not too much trouble?

    I don't know how I can add value to the community when my programming skills are nil, but I certainly am looking for a way to contribute in some meaningful way in order to pay it forward. I think this is the kind of attitude that makes a community awesome and something to be proud of and hold great value. On the other hand, if everyone is so selfish that they will not spend a few minutes to explain something simple and provide an example, a community holds much less value, does it not?
     
  7. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    The problem is that you aren't showing your own attempt. Coders are busy coding and debugging most of the time, and may just not have time to write tutorial code for you. Depending on implementation, what you want could take a few hours or more. Besides, if you really want to learn, you'll pick up a lot more putting in the effort to figure out what you need to know with a bit of research and trial. If you can't even figure out where to start, it might be too advanced for your level of knowledge.

    That said, hang in there, figure it out one step at a time. How is your grid movement script storing its grid? Can you modify that to store whether the terrain is possible? Do you want to define the grid on your own, or figure out some way of automating impassable terrain detection? You could skip the array and use raycasts and colliders. Use box colliders to represent impassable terrain, and shoot a raycast to the next square to see if it's traversable. There are lots of ways to implement any concept.
     
  8. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    Size of the level has no bearing on the complexity, since once the code is written the level size is just data. Doesn't matter if it's a hallway, a small bathroom, or the entire world of Ultima 4.

    You're not asking for an explanation; you're asking for a complete implementation, which is simple, but not easy. It would take a couple of hours to write that program and a day or two to write a tutorial explaining it. What incentive is there for anyone to hand that over to you on a silver platter? There are less taxing ways to help the community, such as answering questions by people who have actually put in some effort into solving their problems and who have specific questions with specific answers, i.e. questions that that can be answered in the space of a forum post.

    Again, you're not seeking an explanation. You're seeking an entire implementation. And as already noted, that will take hours, not minutes.

    If you're programming skills are nil, then start by learning programming. You mentioned in another thread that you knew QBasic at some point. Picking up the basics (no bun intended) should take you no time if that is the case. The C# Yellow Book is free and I see it often recommended to beginners. Also take a deep dive into Unity/Learn as it has a lot of great, novice-friendly material.