Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ozale · Mar 08, 2011 at 11:43 PM · coordinatesmultidimarray

Arrays vs Coordinates

G'day guys, I'm not after code but rather code design advice. I've built the 'chassis' of a maze-navigation game, where the player moves along seemingly in a grid. At each interval of 1 unit, they reach what I am calling a 'junction'. This tells them which directions are available - North, South, East or West. To check which 'junction' the player has reached, I've created an IF statement for each junction, that runs in Update().

I'm considering converting this to use a multidimensional array(or two standard arrays, one for X and one for Z), but since the current system runs so well, and I've had trouble finding any information on multidimensional array use in Unity except vague reports that it used to require a hack, but no longer does.

I'd like to hear other people's opinion on this, as I'm making respectable progress in what is my very first 3D game, and I'd like to refine it as much as possible.

If it's relevant, I plan on creating a browser-embedded version, to enable players to compete for a 'World High Score'.

Cheers
Stew

Comment
Add comment
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

2 Replies

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

Answer by DaveA · Mar 08, 2011 at 11:59 PM

I would first try a 2D array of ints, each int consisting of a bit field where north = 1, south = 2, west = 4, east = 8 and you add those together to get the possible directions at each junction.

However, if you plan on expanding things beyond this, you might also try creating a Junction class, which contains such a bit field int, but could also contain other data, such as what texture to show on the floor there, what things they might find there, etc. Then make the 2D array of Junction.

Comment
Add comment · Show 2 · 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
avatar image Ozale · Mar 09, 2011 at 12:10 AM 0
Share

That sounds good, but when you say 2D, you mean a $$anonymous$$ultidimensional array? There's no mention of multidimensional arrays in the scripting reference. Is it supported?

avatar image DaveA · Mar 09, 2011 at 02:25 AM 0
Share

Yes, see Eric5h5's answer

avatar image
1

Answer by Eric5h5 · Mar 09, 2011 at 12:09 AM

I assume you're using JS; C# multidimensional arrays always worked. In any case, multidimensional arrays in JS now work directly without having to use type inference hacks.

private var maze : int[,]; var xSize = 10; var zSize = 20;

function Start () { maze = new int[xSize, zSize]; }

Note that you can always use a standard 1D array to represent any number of dimensions.

private var maze : int[]; var xSize = 10; var zSize = 20;

function Start () { maze = new int[xSize * zSize]; }

In this case, to access x = 3 and z = 5, you'd do

maze[3 + xSize*5]

As to why you might want to do this when multidimensional arrays exist, it's because 1D arrays are faster even with the extra bit of math. (Note: yes, xSize is correct, not zSize. When specifying rows, you need to multiply by the number of columns, assuming x = columns and z = rows.)

Comment
Add comment · Show 3 · 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
avatar image Ozale · Mar 13, 2011 at 12:18 AM 0
Share

Ok I've edited my question, although it might go unnoticed. I'm tempted to ask a new question, but I'll try this first.

avatar image Eric5h5 · Mar 13, 2011 at 12:41 AM 0
Share

@Ozale: I don't see any edits.

avatar image Ozale · Mar 13, 2011 at 12:03 PM 0
Share

Sorry, I've been playing around with this alot. I have a variation with an Array working now, but on a different question. You can see it here http://answers.unity3d.com/questions/45915/building-a-multidimensional-array-for-grid-movement

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

No one has followed this question yet.

Related Questions

Float to angle coordinate? 1 Answer

JavaScript multidimensional array editing 1 Answer

Find raycast collision coordinates of an impending entire object? 1 Answer

How do I get the x and y coordinates out of Input.mousePosition? 2 Answers

local space and rotation issues 1 Answer


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