In my game you can select multiple units and send them to a destination with a mouse click. however when they try to go to the same place they all collide with each other and spin in circles. how can i make it so that when multiple units go to the same point they don’t try o pile on top of one another.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ClickOn : MonoBehaviour {
private MeshRenderer myRend;
public bool currentlyIsSelected = false;
//public bool ifCanMove = true;
NavMeshAgent nav;
Vector3 destination;
Vector3 curPos;
public GameObject HoverProjector;
// Use this for initialization
void Start () {
myRend = GetComponent<MeshRenderer>();
Camera.main.gameObject.GetComponent<Click>().selectableObjects.Add(this.gameObject);
nav = GetComponent<NavMeshAgent>();
//ifCanMove = true;
}
// Update is called once per frame
void Update () {
if(currentlyIsSelected == true)
{
HoverProjector.SetActive(true);
curPos = transform.position;
if (Input.GetMouseButtonDown(0))
{
Move();
}
}
else
{
HoverProjector.SetActive(false);
}
}
public void ClickMe()
{
Debug.Log("I have been clicked");
}
public void Move()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
destination = hit.point;
// nav.SetDestination(hit.point);
nav.SetDestination(destination);
}
}
}
3343114–261122–ClickOn.cs (1.51 KB)