Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by SecretAgentMango · Sep 08, 2017 at 10:43 PM · arraymovement scriptgridmove

How do I make a map with a 2D array?

So I'm making a top-down 2-D game, and it has grid-based movement (i.e. Pokemon). I've set up box colliders and rigidbodies with the walls, but whenever my player goes up to the wall he ends up just getting stuck inside of it and just sort of vibrates. I understand this is because of my grid-based movement, and I read somewhere that I should create 2D Array to manage my "tiles" in my game. How do I do that? Here's what I was thinking I would start with:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Array : MonoBehaviour {
 
     public static int Row = 10;
     public static int Column = 14;
     private int iCount;
 
     public GameObject[] MyTiles;
 
     public GameObject[,] numbers = new GameObject[Row, Column];
 
     void Start () 
     {
         for (int row = 0; row < Row; row++) 
         {
             for (int column = 0; column < Column; column++) 
             {
                 iCount = 0;
                 numbers [row, column] = MyTiles [iCount];
                 iCount++;
             }
         }
     }
 }

And then I suppose I would add all of my tile sprites into the array MyTiles, but from there I wouldn't know what to do to say something like "If my player's x and y positions are not within these bounds, then don't let him move." Thanks if anyone can help!

Comment
Add comment · Show 1
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 $$anonymous$$ · Sep 08, 2017 at 11:14 PM 0
Share

A simple solution if you don't want to keep track of your tiles in an array would be to linecast one block in front of the player and have a solid object layer that when the linecast hits something, it checks if what its hitting is on your solid layer. If it is you can than disable input in that direction.

1 Reply

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

Answer by TheSOULDev · Sep 08, 2017 at 11:15 PM

First think of how many tile types you're going to have. It would be ideal if you could use 255 as that would greatly conserve memory, but that is fairly inflexible. Let's say that you won't have more than 32k tile types. So, the type of your array will be an ushort. That stands for unsigned short, which is a data type that contains integers between 0 - 2^16 - 1.

Now, think about how large your map can be. Pokemon maps have more than 255 tiles in one dimension for sure, at least newer Pokemon games (unsure about older ones). So, your limits are going to also be 16-bit.

You can define a constant in some static script, which is what I recommend. To do that, create a static script which can hold your variables. And example of what I have would be this:

 using UnityEngine;
 
 public static class Constants
 {
 #region GAME_AMOUNT_CONSTANTS
     public static int EventCount = 8;
 
     public static int ItemCount = 4;
     public static int AriaCount = 60;
     public static int QuestItemCount = 8;
 
     public static int NumberOfMaps = 2;
     public static int NumberOfLanguages = 2;
 
     public static int NumberOfVibrateParts = 30;
 #endregion
 }

I can access these numbers any time I want. Let's say I want to access number of maps. In any script I have, I would just write:

 someValue = Constants.NumberOfMaps;

Why is this useful? Well, you're gonna store your maximum map size here, as arrays are limited in size, unlike Lists which have only 1 limitation - heap size.

So, your Constants script would look something like this now:

 using UnityEngine;
 
 public static class Constants
 {
     public static ushort maxTilesX = 1000;
     public static ushort maxTilesY = 1000;
 }

assuming the maximum amount of X and Y tiles is 1000. You can adjust this.

Now, onto the map data. The way you can save your map is by loading the array you have with tile ID numbers. Let's say:

 public enum TileID
 {
     dirt = 0,
     grass = 1,
     water = 2,
     flowingWater = 3
 }

This is just an example, it can go on for quite a bit. I think the only limit is int size, but I doubt you could type in 2^31 - 1 tile types.

Anyways, let's say you have your map, and you want the 1st row to have 4 adjacent dirt blocks, the 2nd row 2 water blocks, then 2 flowing water, and the 3rd row to have 4 adjacent grass blocks.

You could define the map like this then:

 ushort[,] Map = new ushort[Constants.maxTilesX, Constants.maxTilesY];
 
 Map[0, 0] = Map[1, 0] = Map[2, 0] = Map[3, 0] = (ushort)TileID.dirt;
 Map[0, 1] = Map[1, 1] = (ushort)TileID.water;
 Map[2, 1] = Map[3, 1] = (ushort)TileID.flowingWater;
 Map[0, 2] = Map[1, 2] = Map[2, 2] = Map[3, 2] = (ushort)TileID.grass;

Now, the rest is up to you: you would have to create a program which would let you easily add in these blocks - you can either create a function that will save your map after it analyzes game object positions in the editor, or you can create a seperate program which will, outside of unity, create the array for you (which you can then serialize, OR serialize before and write a script in unity to unpack the data). Either way, the idea is to record tile types ideally via enumerators which hold unique values and save them in an array so that when you load the map, Unity can load the whole array, reconstruct the scene for you, and record the adjacent tile ID's of your player. Then, when attempting to move, you can read the adjacent tile IDs and if they're on a list of ID's you can't walk into, you can play a sound and an in place running animation like in Pokemon, or do nothing.

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

79 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

Related Questions

How can I Stop grid based movement on One or more access? 0 Answers

How to let player move for specific number of times in gird base system.,Implement a move system in grid where player can move for specific number then dies if he did not meet game end/ 0 Answers

Randomize which script is enabled 1 Answer

move a camera to different positions of a sorted array 2 Answers

Listing multiple objects 2 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