- Home /
Need help for Translating a peg in the game...
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
Here what i generated my pegs on the Board..and i want to Translate the selected peg..Am struggling this one any one help.Thanks.
You are only lerping once. You need to lerp on update as it will only move you piece a fraction to the target.
where i have to put that line,it is in update only ,i call the function in update..
Answer by Addyarb · Jun 13, 2015 at 03:16 PM
At line 25, add something like:
else{
MovePegs();
}
You are only causing the peg to "Lerp" for one frame, which is moving it just a minute distance. You need to constantly check the distance, and keep moving it until it has reached it's destination. Calling the function MovePegs(); while it isn't at it's destination will do that.
No, that one also not working ,it also snapping the peg along with the mouse position while i didn't click the button.any other suggestion...
Try putting in a new method, like such:
void $$anonymous$$oveSelectedPeg(GameObject pegTo$$anonymous$$ove, Vector3 targetPosition){
if(!pegTo$$anonymous$$ove) return; //don't run this method if we don't have a GameObject.
if(pegTo$$anonymous$$ove.transform.position != targetposition){
pegTo$$anonymous$$ove.transform.position = Vector3.Slerp(pegTo$$anonymous$$ove.transform.position,targetPosition,Time.deltaTime * 10);
}
else{
return null;
}
}
Then call that method in your $$anonymous$$ovePegs method at line 19.
$$anonymous$$oveSelectedPeg(selectedPeg,targetPosition);
Be sure and put pegTo$$anonymous$$ove and targetPosition as public variables at the top of your script!
Good luck :)
No sir this one also same problem ,it moves only some point distance.. Can i give you this project ,can you check what am doing wrong,this is my first grid system project i taken and am struggling for a while..thanks