Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by bec_bunsen · Feb 16, 2012 at 12:06 AM · array2d gametilemapgridmoving

Moving a character into a 2d int array

Hi dear Unity community :)

I learn javascript through Unity for some months now so i sure have some gaps in my knowledge and i'm kind of stuck in my new project. It's a simple 2D tiled puzzle/plateformer. I'm planning to use @Eric5h5 GridMove and from my reading around here I understand the best way to build my game is to use an int array to store my level. Then here is a great example from @Statement found as an answer here : http://answers.unity3d.com/questions/51945/building-a-multidimensional-array-for-grid-movemen.html

 private var width = 10;
 private var height = 10;
 
 private var grid = [
             [1,1,1,1,1,0,1,1,1,1], 
             [1,0,0,0,1,1,1,0,0,1], 
             [1,1,1,0,0,1,0,0,1,1],
             [1,0,1,1,1,1,1,1,1,0], 
             [1,0,0,0,1,0,1,0,1,0], 
             [1,1,1,1,1,0,1,1,1,1], 
             [0,0,1,0,1,1,1,0,0,1], 
             [1,1,1,1,1,0,1,1,0,1], 
             [1,0,0,1,0,0,0,1,0,1], 
             [1,1,1,1,1,1,1,1,1,1]
         ];
 
 private var edges : Edges[,];
 
 @System.Flags
 enum Edges
 {
     None = 0,
     East = 1, 
     North = 2, 
     West = 4, 
     South = 8,
     // Edit: added Edges.All as shortcut for East|North|West|South
     All = 15
 };
 
 // Let's build the edges so we don't need to do a lot of checking.
 BuildEdges();
 
 function BuildEdges()
 {
     edges = new Edges[width, height];
 
     for (var y = 0; y < height; ++y)
     {
         for (var x = 0; x < width; ++x)
         {                
             edges[x, y] = FindEdges(x, y);       
         }
     }
 }
 
 function FindEdges(x : int, y : int) : Edges
 {
     var edge = Edges.None;
 
     if (Grid(x, y - 1) == 1)
         edge |= Edges.North;
     if (Grid(x - 1, y) == 1)
         edge |= Edges.West;
     if (Grid(x + 1, y) == 1)
         edge |= Edges.East;
     if (Grid(x, y + 1) == 1)
         edge |= Edges.South;
 
     return edge;
 }
 
 function Grid(x : int, y : int) : int
 {
     if (x < 0 || x >= width) return 0;
     if (y < 0 || y >= height) return 0;
     return grid[x][y];
 }
 
 // Use Test as such: 
 // if (Test(x, y, Edges.West))
 //    MoveWest();
 
 function Test(x : int, y : int, direction : Edges) : boolean
 {
     if (x < 0 || x >= width) return false;
     if (y < 0 || y >= height) return false;
 
     return (edges[x, y] & direction) == direction;
 }

Here i don't understand the last part "function Test". Is it written just as an example ? Or is it done for specific purpose ? Do I have to write a function Test for each direction to know if my character can move ?

(edit) Another point, I don't get the use of the enum edges numbers.

Then, if i want to add other tiles functions, for example a ladder or a hole one, do I have to store it as a similar way in an new array ?

Sorry if i did something wrong, it's my first question here ^^'

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 syclamoth · Feb 16, 2012 at 02:25 AM 0
Share

The point of 'Test' is to make sure that you're not moving off the edge of the board. The 'edge' enum allows you to check which edges a given point is adjacent to.

avatar image bec_bunsen · Feb 16, 2012 at 11:56 AM 0
Share

ok then I have to write another script "function Test" to know if my character can move, right ? Sorry i've not been enough precise, but what's the use of the enum edge numbers ?

0 Replies

· Add your reply
  • Sort: 

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

6 People are following this question.

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

Related Questions

Rotating a tilemap with ruletiles 0 Answers

How can i make the Unity2d grid use rounded numbers? 1 Answer

Is there a way to change Tilemap Grid axis direction? 0 Answers

How to set tile from ScriptedTile script? 1 Answer

5 seconds after i hit play a projectile that hasnt been triggered launches and removes the tilemap, HELP 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