Search Unity

New to Unity - Positions for a board Game

Discussion in 'Scripting' started by R.A.E., Apr 24, 2008.

  1. R.A.E.

    R.A.E.

    Joined:
    Apr 24, 2008
    Posts:
    44
    Hi everybody,

    I just bought Unity and start to learn scripting. So please be patient. As I said a newbie writing here :wink:

    For my first project I´ve chosen a little board game.

    For that I need to store a few positons, thought about a array with all the positions at which a prefab will be instantiated.

    Something like this :

    Code (csharp):
    1.  
    2.  
    3. // Array for positons of the holes             
    4. static var position : Vector3(x,y,z) = new Vector3[6];
    5. position[0] = Vector3(0, 0, 10);    // Hole ->1
    6. position[1] = Vector3(-4, 0, 10);   // Hole ->2
    7. position[2] = Vector3(-8, 0, 10);   // Hole ->3
    8. position[3] = Vector3(-12, 0, 10);  // Hole ->4
    9. position[4] = Vector3(-16, 0, 10);  // Hole ->5
    10. position[5] = Vector3(-20, 0, 10);  // Hole ->6
    11.  
    12.  
    13.  
    I´m not 100% sure if that is the right use of Vector3() ( but the cordinates in the brackets represent the positions where i want to instaniate)

    As you can see the only thing that is really changing so i thought about a just changing that dynamically.

    like case 1 x = 0;
    case 2 x = 4;




    Also i need a shuffle function (1-6) i thought of this

    Code (csharp):
    1.  
    2.  
    3. // random function 1-6 for shuffling
    4.     var shuffleResult : int = Random.value * 6;
    5.     // debug message for check  return the shuffleresult
    6.     Debug.Log(shuffleResult.ToString());
    7.  
    8.  

    Thanks

    Marc
     
  2. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    For random numbers, you'll need something like this:

    Code (csharp):
    1. function roll(){
    2.   var ran:int;
    3.   ran = Mathf.Floor(Random.Range(1,6+1));
    4.   return ran;
    5. }
    Plain old Random gives you a number between 0 and 1.

    So we need to use Range.

    But you will still get decimals. Which doesn't work for dice.

    You would never get a 6. The highest number possible is 5.9, So you need to use the Floor method (rounds down to the nearest whole number) so that you don't get decimals.

    Trust me, you don't want Ceiling here.

    And even rounded, just giving it a range of 1 to 6 would give you a number between 0 and 5 so you have to say +1 or else you will never get a 6.

    Also, when you get a 0, the +1 will make it a 1 instead (and so on...)

    This took some time and some headache to find a useful answer. So I thought I would share.

    Source:
    http://www.shawnolson.net/a/789/make-javascript-mathrandom-useful.html
    See it in action here:
    http://thepenry.net/jsrandom.php
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Actually, no, you can use integers with Random.Range. The difference is that the upper bound for integers is non-inclusive, whereas for floats it's inclusive. So "Random.Range(1.0, 6.0)" will return floats between and including 1.0 and 6.0. But "Random.Range(1, 6)" will return integers from 1 through 5. It would be better to write

    Code (csharp):
    1. function roll() : int {
    2.   return Random.Range(1, 6+1);
    3. }
    You'd want a loop for that.

    Code (csharp):
    1. static var position = new Vector3[6];
    2. for (i = 0; i < 6; i++) {
    3.    position[i] = Vector3(-i*4, 0, 10);
    4. }
    This will work, too, instead of Random.Range. Except it will go from 0 through 5 instead of 1-6, but that's actually what you want here because of the array. You could do "var shuffleResult = Random.Range(0, 6);" and get the same thing, which is a bit more clear I think. Also you can just write "print(shuffleResult);" which is faster to type.

    --Eric
     
  4. R.A.E.

    R.A.E.

    Joined:
    Apr 24, 2008
    Posts:
    44
    Thanks a lot for the tips. That is really helpful. Will try it out later keep you updated.

    Marc