How to Improve my Selection Script?
HI there, I'm fairly new to unity and have created a selection script which mostly works as intended apart from a few issues. Any help and advice is greatly accepted and ill give you a hug if you weren't all the way over there.
The First Main Issue is that i cant select a single unit, I know this is down to me using the gameobject tag which selects all the Units with the tag player and because they are all duplicated they are running on the same selection script instead of separate ones.
The Second Issue Combines with the first because it selects all the units it create multiple movepoints depending on how many units there are. Is there a way to only spawn one waypoint if multiple ones go to the same location.
Heres the selection/movement script:
public class Selection_Units : MonoBehaviour
{
bool selected;
bool ismoving;
public GameObject movepoint;
NavMeshAgent Solider;
private GameObject currentmovepoint;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Mouse is down");
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
Debug.Log("Hit " + hitInfo.transform.gameObject.name);
if (hitInfo.transform.gameObject.tag == "player")
{
selected = true;
Debug.Log ("It's working!");
}
else
{
Debug.Log ("nopz");
}
}
else
{
Debug.Log("No hit");
}
Debug.Log("Mouse is down");
}
if (selected == true)
{
CheckKey ();
MovePlayer ();
}
}
private void CheckKey()
{
if (Input.GetMouseButtonDown (1))
{
if (currentmovepoint == null)
{
currentmovepoint = Instantiate (movepoint);
currentmovepoint.transform.position = MovePoint ();
selected = false;
}
else
{
Destroy (currentmovepoint);
}
}
}
Vector3 MovePoint()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit))
{
return hit.point;
}
return Vector3.zero;
}
private void MovePlayer()
{
Solider = this.GetComponent<NavMeshAgent> ();
Solider.SetDestination (currentmovepoint.transform.position);
}
private void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "MovePoint")
{
Destroy (col.gameObject);
}
}
}
Your answer

Follow this Question
Related Questions
I'm having problems instaniating 0 Answers
Bullets are firing in the wrong direction. 1 Answer
Strange bug in a Space Invaders type game 0 Answers
How to delete an instantiated GameObject 2 Answers
How to make a dynamic wall generation 0 Answers