Hello.
I have a “tile” object with several copies from it in the scene.
Now I have a player on one tile, and I can always keep track of which tile is it on (through a script).
Now I want that when the user clicks on a different tile, it moves to it.
However, this is not working, as the player moves to very different positions than the desired position (above the tile clicked on)
Here’s my scene:
And here’s the script that isn’t working:
if(Input.GetMouseButtonDown(0)){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)){
GameObject weHit = hit.transform.gameObject;
if(weHit.transform.tag =="tile"){
//we know its a tile, now check if colWith is in the array of that object we hit!!
if(checkIfNeighbours(colWith, weHit)){
//it's a legit move, now move!
if(!isMoving){
isMoving=true;
Vector3 fromPos = player.transform.localPosition;
Vector3 toPos = fromPos;
toPos.x = weHit.transform.localPosition.x -28.1f;
toPos.z = weHit.transform.localPosition.z + 149.6f;
Debug.Log (toPos.x + " " + toPos.z);
player.transform.localPosition = toPos;
//Vector3.Lerp (fromPos, toPos, Time.deltaTime * speed);
}
isMoving=false;
}
else{
//not a legit move, play sound
}
}
Here, weHit is the gameObject (tile) we clicked on, and player is the cube character.
I got the numbers 28.1 and 149.6 by simply testing the relation between the player’s position in the editor and the cube its’ on position in the editor.
Any help would be appreciated.