Search Unity

Need help with moving objects inside an array

Discussion in 'Scripting' started by NatureRunes, Feb 12, 2019.

  1. NatureRunes

    NatureRunes

    Joined:
    Feb 19, 2016
    Posts:
    14

    Hello so I am currently following a youtube tutorial series on a simple game of checkers that I made models for and want to branch off after I am done.and I am following him exactly but since we are not using the same assets, I am getting issues lining up stuff on my board.

    So far I have this as my code to spawn my pieces on the board:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CheckersBoard : MonoBehaviour {
    6.  
    7.     public Piece[,] pieces = new Piece[8, 8];
    8.     public GameObject WhitePiecePrefab;
    9.     public GameObject BlackPiecePrefab;
    10.  
    11.     public Vector3 boardOffset = new Vector3(-3, 0, 5);
    12.     public Vector3 pieceOffset = new Vector3(0, 0, -2);
    13.  
    14.     private void Start()
    15.     {
    16.         GenerateBoard();
    17.     }
    18.  
    19.     private void GenerateBoard()
    20.     {
    21.         // Generate White Team
    22.         for(int y = 0; y < 3; y++)
    23.         {
    24.             for(int x = 0; x < 8; x += 2)
    25.             {
    26.                 // Generate Our Piece
    27.                 GeneratePiece(x, y);
    28.             }
    29.         }
    30.     }
    31.  
    32.     private void GeneratePiece(int x, int y)
    33.     {
    34.         GameObject go = Instantiate(WhitePiecePrefab) as GameObject;
    35.         go.transform.SetParent(transform);
    36.         Piece p = go.GetComponent<Piece>();
    37.         pieces[x, y] = p;
    38.         MovePiece(p, x, y);
    39.     }
    40.  
    41.     private void MovePiece(Piece p, int x, int y)
    42.     {
    43.         p.transform.position = (Vector3.right * x) + (Vector3.forward * y) + boardOffset + pieceOffset;
    44.     }
    45.  
    46. }
    47.  

    I have adjusted the values like crazy but this is the best I can get.
    And this is the result I get:

    https://gyazo.com/aeaf30e581a8e488e4afcf1ecbffda14

    Does anyone know a better way to set the grid up? Sorry if I am missing information, I can provide more.
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    It looks like his code is assuming the grid is 1x1(unity-units) for each block, if your model isn't the same size the code won't work.

    I recommend using the assets he provided and "modeling over them", tracing them if you will, this will make sure you have the same size but with your art work instead of his.
    If you use a different size I predict you'll also have troubles with the movement code and such and you'll need to scale the entire in-code grid.
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Forward vector is Z not Y.
    So probably either you want change Y to Z
    or
    change Vector3.forward to Vector3.up

    Edit:
    However, I haven't watched full video, just skip minutes forward, but seams you did follow as per video. So not sure what is wrong otherwise.
     
  4. NatureRunes

    NatureRunes

    Joined:
    Feb 19, 2016
    Posts:
    14
    Thats what I thought! His video its doing it correctly even though its on a different axis. All I need them to do is split apart more haha. I'll keep trying to figure it out though, but I'm glad someone pointed it out so im not crazy.
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I assume he is only representing this in an XY fashion in-code(I do this too all the time), the code looks to be correct.
    if you look at the last line he is using .forward and .right
     
  6. NatureRunes

    NatureRunes

    Joined:
    Feb 19, 2016
    Posts:
    14
    The board is the provided one from his tutorial and I modeled my own tabled around it. That's why I'm confused on why this isn't working.
     
  7. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    If you put the two tables on each other are they identical in unity also?
    Doesn't that mean that his board doesn't work too?
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Yep. Possibly your suggestions are better than my, in this case :p
     
  9. NatureRunes

    NatureRunes

    Joined:
    Feb 19, 2016
    Posts:
    14
    The board is my Gamemaster kinda. the table doesn't serve any function besides the background, pretend its not even there. I just don't know why the board won't get setup when its the exact same model imported in haha

    edit: Theres only one board, I don't know what you mean by two tables.
     
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    I may be wrong, but I think @SparrowsNest means simply a duplicate of the table, with generated pieces?
    Worth try a shot, to see if it work.
     
  11. NatureRunes

    NatureRunes

    Joined:
    Feb 19, 2016
    Posts:
    14
  12. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    What does "the board is my gamemaster" means?

    I mean if you put the table that the tutorial gives you in the scene together with the table you modeled, are they the same size?

    I realize it's only the background and doesn't "serve and function"(besides showing the players the grid).

    "I just don't know why the board won't get setup when its the exact same model imported in haha"
    So it won't work even with the table the tutorial gives you?
    better re-watch the tutorial and see if you missed anything.

    edit: no idea what this red grid is
     
  13. NatureRunes

    NatureRunes

    Joined:
    Feb 19, 2016
    Posts:
    14
    Ok, so. Board as the gamemaster I meant all my scripts are going to be attached to it, but I don't think that's relevant. And yes, The table from the tutorial lines up with the table I made. Also, I meant the table is the background, not the board itself ( Sorry :( )
    And that's why I'm confused, I can't even get it working with the assets he provided. See my screenshot above though, I think that might be an issue? I'm working at like a -90 angle or something.

    The red grid is the unity grid that I recolored cause I have troubles seeing the white sometimes.

    (Not trying to sound mean, thanks alot for helping! :) )
     
  14. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Oh yeah, I do this too, I just didn't think of that because it is indeed not relevant, haha.

    Then I would go back, re-watch the tutorial and see that his code is identical to yours. (or your to his, technically, but nvm)

    Nothing to be sorry about, lol

    Looks like the Y axis is pointing up, like it should, I don't see why you think you're off by 90 degrees.
    the only thing is it looks like the grid isn't 1X1, but that is probably because of the perspective.

    0% of what you said was even remotely mean.
    that's what we're here for.
    that and procrastinating our own developments, haha.
     
  15. NatureRunes

    NatureRunes

    Joined:
    Feb 19, 2016
    Posts:
    14
    I thought I was -90 off because I guess I thought XYZ was where Z was up and down like in blender. I have alot of learning to do haha novice mistakes I suppose. I am currently redoing the whole video so far, hopefully I don't encounter it again :( I'm not sure what I did wrong but something clearly is amiss.

    I don't know what the heck I did wrong the first times but

    https://gyazo.com/dde2d16341fe95741394c9f393024c47

    :) Thanks for all the help. This place is pretty useful, Ive been reading other threads before I decided to post for first time lol
     
    Last edited: Feb 12, 2019