Moving an object on a Grid
I am making a grid based Turn based game and i can't seem to be able to move the object anywhere, I can select the object and store it in a variable, but when i try to change it's transform.position, it doesn't work, I am a novice programmer so i haven't the slightest idea on how to make this work, My code will be shown below.
using UnityEngine; using System.Collections.Generic; using UnityEngine.UI; using System.Linq; using Random = UnityEngine.Random;
public class PlayerBehaviour : MonoBehaviour { public int ActionsPerTurn; public Boardmanager boardManager; private Vector3 SelectedPosition; private GameObject SelectedItem; private GameObject Mbutton; private GameObject Bbutton; private GameObject Abutton; //Returns the type of object at a clicked position GameObject ClickSelect() { RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit)
{
return hit.transform.gameObject;
}
else return null;
}
Vector3 ClickPosition()
{
// collects the world position of a mouse click
Vector3 Position = new Vector3 (Camera.main.ScreenToWorldPoint(Input.mousePosition).x,Camera.main.ScreenToWorldPoint(Input.mousePosition).y, 0f);
return Position;
}
void Start () {
boardManager = GetComponent<Boardmanager> ();
Mbutton = GameObject.FindGameObjectWithTag("SpawnMSoldier");
Abutton = GameObject.FindGameObjectWithTag("SpawnASoldier");
Bbutton = GameObject.FindGameObjectWithTag("SpawnBSoldier");
Mbutton.gameObject.SetActive(false);
Bbutton.gameObject.SetActive(false);
Abutton.gameObject.SetActive(false);
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
SelectedItem = ClickSelect();
Debug.Log(ClickSelect());
if (SelectedItem == GameObject.FindGameObjectWithTag("PlayerHQ"))
{
Mbutton.gameObject.SetActive(true);
Bbutton.gameObject.SetActive(true);
Abutton.gameObject.SetActive(true);
}
// if the object clicked was a Marksman
else if(SelectedItem == GameObject.FindGameObjectWithTag("PlayerSoldierMarksman"))
{
//if the mouse is clicked down after the object is selected
if (Input.GetMouseButtonDown(0))
{
//setting a variable to ClickPosition to prevent recalling of the method
SelectedPosition = ClickPosition();
//if the selected position is not occupied by a game object
if (checkposition(SelectedPosition) == false)
{
//move the object to a position passing a parameter of SelectedPosition
Movement(SelectedPosition);
}
//else the movement does not occur, and a debug of a block is created
else
Debug.Log("Movement Blocked");
}
}
}
}
void Movement(Vector3 Position)
{
//Sets the Selected object as the GameObject clicked on
GameObject SelectedOb = SelectedItem;
//Sets the Selected Objects Position to the Position clicked on
SelectedOb.transform.position = Position;
}
public bool checkposition(Vector3 Position)
{
List<List<GameObject>> allActive = new List<List<GameObject>>();
allActive.Add(GameObject.FindGameObjectsWithTag("Blocker").ToList());
allActive.Add(GameObject.FindGameObjectsWithTag("PlayerHQ").ToList());
allActive.Add(GameObject.FindGameObjectsWithTag("PlayerSoldierMarksman").ToList());
allActive.Add(GameObject.FindGameObjectsWithTag("PlayerSoldierBasic").ToList());
allActive.Add(GameObject.FindGameObjectsWithTag("PlayerSoldierArmored").ToList());
allActive.Add(GameObject.FindGameObjectsWithTag("PlayerUnitRailgun").ToList());
allActive.Add(GameObject.FindGameObjectsWithTag("EnemySoldierMarksman").ToList());
allActive.Add(GameObject.FindGameObjectsWithTag("EnemySoldierBasic").ToList());
allActive.Add(GameObject.FindGameObjectsWithTag("EnemySoldierArmored").ToList());
allActive.Add(GameObject.FindGameObjectsWithTag("EnemyUnitRailgun").ToList());
allActive.Add(GameObject.FindGameObjectsWithTag("EnemyHQ").ToList());
if (allActive.Any(activeItem => activeItem.Any(activeGameObject => activeGameObject.transform.position == Position)))
{
return true;
}
return false;
}
}
Answer by homanhquan · May 19, 2017 at 09:22 AM
My solution is: First need get GameObject to move, and Position clicked on, then you can using Vector3.MoveTowards to move your gameObject.