- Home /
Creating Simple Peg Solitaire Game in unity.
Hi am new to unity and i know little much scripting in C# ,am creating a simple puzzle game called Peg Solitaire
My question about scripting ,i arranged the pegs as shown in the image by array grid system here is My code
public GameObject prefab;
public int gridX = 7;
public int gridY = 7;
public float spacing = 6.5f;
private GameObject marbleGRP;
//public Vector2 slot = new Vector2();
private int [,] grid = new int[,]
{
{0, 0, 1, 1, 1, 0, 0},
{0, 0, 1, 1, 1, 0, 0},
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1},
{0, 0, 1, 1, 1, 0, 0},
{0, 0, 1, 1, 1, 0, 0}
};
void Start()
{
marbleGRP = GameObject.Find ("Marbles_GRP");
for (int y=0; y < grid.GetLength(0); y++)
{
for (int x=0; x < grid.GetLength(0); x++)
{
if(grid[y , x] == 1)
{
GameObject gridPlane = (GameObject)Instantiate(prefab);
gridPlane.transform.position = new Vector3(gridPlane.transform.position.x + x-3,gridPlane.transform.position.y + y-3,
gridPlane.transform.position.z)*spacing;
gridPlane.transform.parent = marbleGRP.transform;
}
//slot = new Vector2(x-gridX/2,y-gridY/2);
}
}
}
void Update ()
{
if (Input.touchCount > 0)
{
if (Input.touches [0].phase == TouchPhase.Began)
{
RaycastHit2D hit;
Vector3 pos = Camera.main.ScreenToWorldPoint (Input.touches [0].position);
//if(Physics2D.Raycast(Camera.main.ScreenPointToRay(Input.touches[0].position),out hit))
if (Physics2D.Raycast (pos, Vector2.zero))
{
print (Input.touches [0].position);
}
}
}
}
}
I want a little help in scripting ,How to select one peg and move it to empty slot by touching it, Please help me.Am making for Android in C#.Thanks.
Just a comment, the 4 2x2 corners cannot be selected, so you might want to put -999 into those elements in your 2d grid. Also, rather than doing ray casting, just look at the coordinate that the user has touched, and select the peg that's at that position. Have a bool variable that says a peg has been chosen, and store the grid position. Then, if the user makes a touch on a grid item with zero, and the bool is true, then move the peg.
Can you please write a simple code in a brief ,that i can catch the script,thanks
Can any one please help me for this game am new to Scripting..
Your answer
![](https://koobas.hobune.stream/wayback/20220613200454im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I fix these two errors in my Number Wizard Game? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Fourier transform rhytm game implementation? 1 Answer
Question about FindObjectsOfType 1 Answer