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 /
  • Help Room /
avatar image
1
Question by Soulzii · Nov 30, 2015 at 04:48 PM · arraycolorprojectilepattern

Making patterns in a 2D array

I want to make patterns using projectors at the bottom of the screen that shoot projectiles up in a pattern.

Now I'm not sure what's the best approach for it, but right now what I'm trying to do is using a 2D array to store the pattern in, but I also want to be able to assign a color to each 'projectile'. Also I'm trying to look for a better way to assign the pattern to an array other than hardcoding it. By maybe loading it in but I'm not sure how to do that.

Here's an example of how It's supposed to shoot projecitles, where the X is where he doesn't shoot and O is where it shoots

XXXOOOXXX

OOOXXXOOO

XXXOOOXXX

XOOOXOOOX

XXOOOOOXX

And in stead of putting every single point in an array by hand, I was wondering if there was a way to put in in a file somewhere and load patterns in. As well as maybe being able to assign colors to each projectile.

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
1
Best Answer

Answer by Onithec4t · Nov 30, 2015 at 05:20 PM

You can create a projectile class like this:

 public class Projectile{
     bool shoot;
     Color color;
 }

and make an array or a list of this object, like

 List<Projectile> projectiles = new List<Projectile>();

Then, you can read your data from a csv file with a script like this: http://wiki.unity3d.com/index.php?title=CSVReader

Or you can build a custom inspector to insert your data.

Comment
Add comment · Show 5 · 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 Soulzii · Nov 30, 2015 at 05:42 PM 0
Share

At this point I have 11 'Projectors' lined up horizontally and I'm trying to make them all have a list of projectiles to shoot, maximum 11 $$anonymous$$inimum 6 projectiles in a loop. So basically I would have a 2D array of [11,6] - [11,11]. $$anonymous$$y problem is that I don't really know precisely how to get a list of Projectiles filled for every projector, in a simple way other than hardcoding it.

avatar image Onithec4t Soulzii · Nov 30, 2015 at 05:48 PM 0
Share

I'm not sure if I understood, but maybe you wanna make a list of list, like this?

List> projectiles = new List>();

BTW you can consider the procedural way, using a Random.Range(x,y) and u can control just the x and the y

avatar image Soulzii Onithec4t · Nov 30, 2015 at 05:55 PM 0
Share

Projectors

Those are the projectors I have lined up, and they all need to shoot projectiles up. They all shoot at the same time (If there's something to shoot in their array, so they all shoot the next item in the array at the same time)

So I hope that makes it a little bit clearer of what I'm trying to make

5.png (8.3 kB)
Show more comments
avatar image Soulzii · Nov 30, 2015 at 07:03 PM 0
Share

If I do what you said in the latest reply, I have to make an object of every individual projectile of the pattern, which is not handy, how can I do this better? How can I assign the values to a Projecitile easier

avatar image
1

Answer by Fattie · Nov 30, 2015 at 07:21 PM

It's incredibly hard to do this, unless you're a naturally gifted programmer and can "think in algorithms".

Here's an example of a routine that makes really beautiful patterns.

In answer to your specific question, sure, deal in int[,] to pass around such concepts.

"look for a better way to assign the pattern to an array other than hardcoding it" Sure, start with simple routines which do things like "mirror" patterns from one of your quadrants.

 /*
 Makes pretty patterns .. in the abstract
 */
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public static class Pretty
     {
     
     public static int ColorInt()    // just a color code, 1 to 10
         {
         return Random.Range(1,11);
         }
     
     // ChallengePattern is a random pattern with only one color having three-count
     // (for the most challenge) the others have a higher count.
     // Compare PatternOfInts which is simple and symmetrical (example use: foyer)
     
     public static int[,] ChallengePattern(int kount)
         {
         if ( kount < 4 ) {Debug.Log("just too hard"); Debug.Break();}
         
         int fullCountOfItems = kount*kount;
         
         // step one, make a list of our colors in a random order.
         // in fact, we know that the codes range from 1 to 10
         
         int[] orderOfColors = Pretty.TheNumbers1To10Shuffled();
         
         int[] countOfColors = Pretty.DecideCountsTotalBeing( fullCountOfItems );
         
         // attention...
         if ( countOfColors.SumArray() != fullCountOfItems )
             {Debug.Log("Algorithm woe, oh oh."); Debug.Break();}
         
         // don't forget to just let the GemInfo know what is happening:
         Grid.gemInfo.RememberCounts( orderOfColors, countOfColors );
         
         // now simply load a linear list like that.
         
         List<int> linear = new List<int>();
         for ( int t=0; t<10; ++t )
             {
             int whichColor = orderOfColors[t];
             int howManyOfThatColor = countOfColors[t];
             for (int k=0; k< howManyOfThatColor; ++k )
                 linear.Add( whichColor );
             }
         
         
         countOfColors.DebugShow();
         linear.ToArray().DebugShow();
         // finally, merely shuffle that linear list, and load to the square
         
         linear = linear.OrderBy(Rx => Random.value).ToList();
         
         linear.ToArray().DebugShow();
         
         int[,] rr = linear.ToArray().Squareize();
         
         return rr;
         }
     
     public static int[] DecideCountsTotalBeing(int desiredTotal)
         {
         // for example, if the total is 50, we might go..
         // 3, 7, 10, 30
         // or perhaps
         // 3, 5, 12, 15, 15
         // we do want to START WITH A THREE because that's how the game works.
         // TBC we "know" there are ten slots (at most),
         // (the game simply has ten colors and that's it)
         
         if ( desiredTotal < 20 ) {Debug.Log("just too hard"); Debug.Break();}
         
         int[] result = new int[10];
         
         int traverse = 0;
         int remaining = desiredTotal;
         
         // we always start with 3!!
         
         int thisTime = 3;
         
         result[traverse] = thisTime;
         ++traverse;
         remaining -= 3;
         
         // the second one (only) can be reasonably small
         
         thisTime = Random.Range(5,9);
         
         result[traverse] = thisTime;
         ++traverse;
         remaining -= thisTime;
         
         // after this, ensure they are at least 7 in size
         
         while ( remaining > 7 && traverse < 10 )
             {
             // thisTime = Random.Range(9,20);        // means 9 through 19
             thisTime = Random.Range(9,17);        // means 9 through 16
             // that value tunes well...
             
             if ( thisTime > remaining )
                 thisTime = remaining;
             
             result[traverse] = thisTime;
             ++traverse;
             remaining -= thisTime;
             }
         
         // ensure that we got enough.
         
         int total = result.SumArray();
         
         if ( total < desiredTotal )
             {
             // Note that if we need to adjust one, we just adjust the "fourth one".
             // It makes absolutely no difference which one you adjust,
             // but it should be one that is not-zero.
             // the last one is often zero, so, don't just adjust the last one.
             // The adjustment number might be small -- say "2" -- in which case
             // if you apply it to a zero item, you'd get a small count.
             // (Of course, do not adjust the special three-count item, ie item zero.)
             
             int stillNeeded = desiredTotal - total;
             int lastOne = result[3];
             // in this algorithm, the fourth one works perfectly in all cases.
             lastOne = lastOne + stillNeeded;
             result[3] = lastOne;
             }
         
         return result;
         }
     
     public static int[] TheNumbers1To10Shuffled()
         {
         List<int> ints = new List<int>();
         for (int load=1; load<=10; ++load)
             ints.Add(load);
         
         return ints.OrderBy(Rx => Random.value).ToArray();
         
         // to unit test, what about...
         // Pretty.TheNumbers1To10Shuffled().DebugShow();
         }
     
     public static int[] IndicesUpToWhateverShuffled( int whatever )
         {
         List<int> ints = new List<int>();
         for (int load=0; load<whatever; ++load)
             ints.Add(load);
         
         return ints.OrderBy(Rx => Random.value).ToArray();
         
         // to unit test, what about...
         // Pretty.TheNumbers1To10Shuffled().DebugShow();
         }
     
     // PatternOfInts is a simple symmetrical pattern
     
     public static int[,] PatternOfInts(int kount)
         {
         // identical to PatternWithStrings,
         // but result is ints 1 to 10 (matching our available gem codes)
         
         if ( kount < 1 ) { Debug.Log("woe"); Debug.Break(); }
         
         int[,] rr = new int[kount,kount];
         
         int half = kount/2;
         if (kount % 2 > 0) ++half;
         
         int highestIndex = kount-1;
         
         for ( int across = 0; across < half; ++across )
             for ( int down = 0; down < half; ++down )
                 rr[across,down] = Pretty.ColorInt();
         
         
         // in fact, diagonally mirror that quadrant...
         
         for ( int across = 0; across < half; ++across )
             for ( int down = 0; down < half; ++down )
                 {
                 if ( across >= down )
                     rr[down,across] = rr[across,down];
                 }
         
         // in fact, make a diagonal line always
         
         for ( int across = 0; across < half; ++across )
             for ( int down = 0; down < half; ++down )
                 {
                 if ( across == down )
                     rr[across,down] = rr[0,0];
                 }
         
         // fill the other three qudrants
         
         // mirror across...
         
         for ( int across = 0; across < half; ++across )
             for ( int down = 0; down < half; ++down )
                 rr[ highestIndex -across, down ] = rr[ across, down ];
         
         // mirror down...
         
         for ( int across = 0; across < kount; ++across )
             for ( int down = 0; down < half; ++down )
                 rr[ across, highestIndex -down ] = rr[ across, down ];
         
         return rr;
         }
     
     public static void Log( this string[,] squarray )
         {
         int kount = squarray.GetLength(0);
         
         string res = "\n\n";
         for ( int across = 0; across < kount; ++across )
             {
             string line = "";
             for ( int down = 0; down < kount; ++down )
                 line = line +" " +squarray[across,down];
             res = res + line + "\n";
             }
         res = res + "\n\n";
         
         Debug.Log(res);
         }
     
     }










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

34 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

Related Questions

Unity List of Color array not showing in inspector? 3 Answers

Setting an Objects Colour using a variable Color from an array? 0 Answers

Sprite renderer colors + Instigate 1 Answer

Creating an array of Color32 arrays 1 Answer

Get value from array in unity: debug works, value's changing, but color not changing 0 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