How to move a game object to a position after selecting it
I'm making a TBS game in which the player spawns units and moves them towards the enemy base. I have the ability to spawn units now, but i have not even the slightest idea on how to get them to move, im drawing a complete blank, my first objective would be to see if the user clicked a unit, then to collect data from a second click somewhere else within range of the movement, then if the click was a game object, and the second click was within movement range, then move the object to that position, my code will be posted below with the movement function highlighted by comments, all i need is just to get the start of the function going, i'm just drawing a blank on how to do this
using UnityEngine; using System.Collections.Generic; using UnityEngine.UI; using System.Linq; using Random = UnityEngine.Random;
public class PlayerBehavior : MonoBehaviour { //public int ActionsPerTurn; public BoardManager boardManager;
private Vector3 SelectedPosition;
private GameObject Mbutton;
private GameObject Bbutton;
private GameObject Abutton;
GameObject ClickSelect()
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit)
{
return hit.transform.gameObject;
}
else return null;
}
void Start () {
boardManager = GetComponent<BoardManager> ();
SelectedPosition = ClickSelect ().transform.position;
Mbutton = GameObject.FindGameObjectWithTag("SpawnMsoldier");
Abutton = GameObject.FindGameObjectWithTag("SpawnAsoldier");
Bbutton = GameObject.FindGameObjectWithTag("SpawnBSoldier");
Mbutton.gameObject.SetActive(false);
Bbutton.gameObject.SetActive(false);
Abutton.gameObject.SetActive(false);
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
ClickSelect();
Debug.Log(ClickSelect());
if (ClickSelect() == GameObject.FindGameObjectWithTag("PlayerHQ"))
{
Mbutton.gameObject.SetActive(true);
Bbutton.gameObject.SetActive(true);
Abutton.gameObject.SetActive(true);
}
/*else if (ClickSelect() != GameObject.FindGameObjectWithTag("PlayerHQ"))
{
Mbutton.gameObject.SetActive(false);
Bbutton.gameObject.SetActive(false);
Abutton.gameObject.SetActive(false);
}*/
Movement(SelectedPosition);
}
}
//This is the movement function, i tried a few things, but again i have no idea what i'm doing
Vector2 Movement(Vector3 unitPosition)
{
Vector2 newPosition;
unitPosition = ClickSelect ().transform.position;
if (checkposition (unitPosition) == true) {
newPosition = ClickSelect ().transform.position;
Debug.Log ("Satement Entered");
}
else {
newPosition = unitPosition;
}
return newPosition;
}
public bool checkposition(Vector3 Position)
{
List<List<GameObject>> allActive = new List<List<GameObject>>();
allActive.Add(GameObject.FindGameObjectsWithTag("Mountain").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)))
{
Debug.Log("Spawn Blocked");
return true;
}
return false;
}
}