Hi ,I am creating a peg solitaire game ,i almost completed,but i need some help how i Translate the peg ,where i clicked… Here is my code…
public int Width;
public int Height;
public GameObject Peg;
public GameObject[,] grid ;
public bool isSelected;
private GameObject selectedPeg;
public float duration = 50.0f;
//List<GameObject> myList = new List<GameObject>();
//List<GameObject> tList = new List<GameObject>();
void Start ()
{
grid = new GameObject[Width, Height];
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
GameObject go =GameObject.Instantiate(Peg) as GameObject;
Vector3 position = new Vector3(x, y, 0);
go.transform.position = position;
grid[x, y] = go;
GameObject GridGB = GameObject.Find ("Marbles_GRP");
go.transform.parent = GridGB.transform;
//myList.Add(go);
//tList.Add(go);
//print(myList.Contains(go));
}
}
//Down side
Destroy (grid [0, 0]);//myList.Remove (grid [0, 0]);
Destroy (grid [0, 1]);//myList.Remove (grid [0, 1]);
Destroy (grid [0, 5]);//myList.Remove (grid [0, 5]);
Destroy (grid [0, 6]);//myList.Remove (grid [0, 6]);
Destroy (grid [1, 0]);//myList.Remove (grid [1, 0]);
Destroy (grid [1, 1]);//myList.Remove (grid [1, 1]);
Destroy (grid [1, 5]);//myList.Remove (grid [1, 5]);
Destroy (grid [1, 6]);//myList.Remove (grid [1, 6]);
//Up side
Destroy (grid [6, 0]);//myList.Remove (grid [6, 0]);
Destroy (grid [6, 1]);//myList.Remove (grid [6, 1]);
Destroy (grid [6, 5]);//myList.Remove (grid [6, 5]);
Destroy (grid [6, 6]);//myList.Remove (grid [6, 6]);
Destroy (grid [5, 0]);//myList.Remove (grid [5, 0]);
Destroy (grid [5, 1]);//myList.Remove (grid [5, 1]);
Destroy (grid [5, 5]);//myList.Remove (grid [5, 5]);
Destroy (grid [5, 6]);//myList.Remove (grid [5, 6]);
//middle
Destroy (grid [3, 3]);//myList.Remove (grid [3, 3]);
//print(myList.Count);
}
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
SelectPegs();
}
if(Input.GetMouseButtonDown(1))
{
MovePegs();
}
}
void SelectPegs()
{
Vector3 mPosition =Camera.main.ScreenToWorldPoint(Input.mousePosition);
int x = (int)(mPosition.x +0.5f);
int y = (int)(mPosition.y +0.5f);
for(int _x = 0; _x < Width; _x++)
{
for(int _y = 0; _y < Height; _y++)
{
GameObject go = grid[_x, _y];
if(go!=null){
go.transform.localScale = new Vector2(2f,2f);
isSelected=false;
}
}
}
if(x >=0 && y >=0 && x < Width && y < Height)
{
GameObject go = grid[x,y];
if(go!=null){
go.transform.localScale = new Vector2(2.3f,2.3f);
isSelected=true;
selectedPeg= go;
selectedPeg.tag = "SelectedTag";
}
}
}
void MovePegs()
{
if(isSelected == true)
{
//foreach (GameObject obj in myList)
{
//if(obj!=null )
{
Vector2 targetPosition =Camera.main.ScreenToWorldPoint(Input.mousePosition);
int _x = (int)(targetPosition.x +0.5f);
int _y = (int)(targetPosition.y +0.5f);
targetPosition.x=(float)(Mathf.Round(targetPosition.x));
targetPosition.y=(float)(Mathf.Round(targetPosition.y));
if(Vector2.Distance(targetPosition,selectedPeg.transform.position)==2.0f)
{
//selectedPeg.transform.position=targetPosition;
//This Line Is not working //
selectedPeg.transform.position = Vector2.Lerp(selectedPeg.transform.position, targetPosition, 20/(duration*(Vector2.Distance(selectedPeg.transform.position, targetPosition))));
selectedPeg.transform.localScale = new Vector2(2f,2f);
isSelected=false;
grid[_x, _y] = selectedPeg;
}
}
}
}
}
}
When am Right clicking the peg moves a little instead of translate.Thanks