1
votes

i got this script thats selects and moves units like an rts game all seems fine untill around 12 seconds pass the game dosnt freeze just nothing in the script is then working.It should put the units selected into the list and activate a highlight child object but it seems that after clicking on the object,when some time has passed, it stops working.

here is the code:

bool flag = false;
NavMeshAgent agent;
[SerializeField]
List<Transform> selected_units = new List<Transform>();
public Camera cam;

public Vector3 v2;
private RaycastHit hit;
void Update()
{
    

    if (Input.GetMouseButtonDown(0))
    {

        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
       
        if (Physics.Raycast(ray, out hit))
        {
            
            if (hit.collider.gameObject.CompareTag("unit"))
            {
                flag = true;
                SelectUnits(hit.transform, Input.GetKey(KeyCode.LeftShift));
                v2 = hit.point;
                
                hit.collider.transform.GetChild(3).gameObject.SetActive(true);

            }
            else
            {
                
                DeselectUnits();
                flag = false;

            }


        }
    }
    else if (Input.GetMouseButtonDown(1) && flag == true)
    {

        Ray ray = cam.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit))
        {
            Move(hit.point);
        }
    }
}
private void Move(Vector3 hit)
{
    for(int i = 0; i < selected_units.Count; i++)
    {
        agent = selected_units[i].GetComponent<NavMeshAgent>();
        v2 = hit;
        agent.SetDestination(v2);
        
    }
}




private void SelectUnits(Transform unitt, bool isMultiselect)
{
    
    if (isMultiselect == false)
    {
        
        DeselectUnits();
        
    }
    selected_units.Add(unitt);
    


   

}
private void DeselectUnits()
{
    for(int i = 0; i < selected_units.Count; i++)
    {
        selected_units[i].transform.GetChild(3).gameObject.SetActive(false);
        
        
    }
    selected_units.Clear();


}
1

1 Answers

0
votes

Use Input.GetMouseButton()... instead of Input.GetMouseButtonDown...

The fist one return True when button has been pressed and not released. The second one return True during the frame the user pressed down button.