Search Unity

2D Maze - Wall Collisions

Discussion in '2D' started by jwld, Nov 24, 2015.

  1. jwld

    jwld

    Joined:
    Sep 17, 2015
    Posts:
    12
    Hi, I'm working on a simple game that involves moving a snake / worm round a maze - I've attached a screenshot of the game so far, with a basic maze layout.

    The worm's position is adjusted by 1 x or y coordinate each time the player presses a direction key, using this kind of code:

    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
    transform.eulerAngles = newVector3(0, 0, 0);
    transform.position += newVector3(0, 1, 0);
    }
    etc.

    I plan on using a raycast to detect collisions with other objects in the maze, but I'm not sure what the best method is to detect the walls of the maze.

    One option is to just add edge / box colliders to each section of wall and use a raycast to detect them as well, however adding all these colliders seems like a pain (especially since you can't input exact coordinates for an edge collider for some reason). I plan on changing the maze design, so wouldn't want to have to do this each time.

    Another option I've come across is using GetPixel to detect the colour at each coordinate. If I could get this to work, I could scan through every coordinate in the maze at the start of the game, and if it found a black pixel, add this coordinate to a list of no-go locations, which I could check against in the code above.

    Ideally I'd like to get the second option to work, but am getting a bit confused when it comes to implementing it. Could anyone possibly explain whether it's possible, and how I might go about doing so?

    Or are there any other easier options?

    Thanks in advance...!

    *edit - accidentally uploaded the screenshot twice
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
  3. jwld

    jwld

    Joined:
    Sep 17, 2015
    Posts:
    12
    Sorry I should've been more specific. I mean in the inspector, which is what I'd be using to set up all the edge colliders if I used that approach (although ideally I'd rather not have to!).
     

    Attached Files:

  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
    Entering it all in the inspector would be tedious. Can you not edit it in the scene-view and use vertex snapping?
     
  5. jwld

    jwld

    Joined:
    Sep 17, 2015
    Posts:
    12
    That sounds like it would be much easier. How do you get snapping to work with colliders though? If I hit Cmd while in 'edit collider' mode it just removes the node - is there another way to snap?

    Re. the second approach, is there any way you know of to scan each coordinate (within a certain range) and detect what colour is at each position?