I’ve created a small class of five cubes and I want to be able to move them all independently and at the same time. So far, I can move all of them to the same exact series of waypoints, I simply “right click” and can add any number of waypoints.
BUT…I want to move them to different waypoints and I want to control each cube separately. I’m having a heckuva time figuring this out. Thanks for any suggestions.
void Start ()
{
for (int i = 0; i < 5; i++)
{
///// Creates Member of Class
CombatUnit blueUnit = new CombatUnit();
///// Creates Cube
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
///// Location of Cube selected randomly
cube.transform.position = new Vector3(Random.Range(5f,25f), 0,Random.Range(5f,25f));
///// Adds movement script to cube
cube.AddComponent<MoveTest>();
}
}
// Update is called once per frame
void Update() { }
public class CombatUnit
{
public int UnitID { get; set; }
}
}
//////////////
public class MoveTest : MonoBehaviour
{
public float speed;
public List targetPosition = new List();
void Start ()
{
speed = 5; ///// This is temporary unit speed
}
void Update ()
{
///// Click to get ID of unit that will move
if(Input.GetMouseButtonDown(0))
{
RaycastHit hit1;
Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast (ray1, out hit1))
{
BoxCollider bc = hit1.collider as BoxCollider;
if (bc != null)
{
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit1, Mathf.Infinity))
{
Debug.Log(gameObject);
}
}
else
{
Debug.Log("Miss");
}
}
}
///// Creates a waypoint/location if the if the unit/object has arrived at last waypoint (to prevent system errors)
///// This also occurs if the unit/object has not been issued any movement orders
///// If there have been no right clicks of the mouse, this creates a waypoint at units own location
if (targetPosition.Count <1)
{
targetPosition.Add(this.transform.position);
}
///// Moves object towards First waypoint
transform.position = Vector3.MoveTowards(transform.position, targetPosition[0], speed * Time.deltaTime);
///// Gets Rid of waypoint if unit/object gets really close to waypoint
///// When the zero index is eliminated, the next lower list[index] becomes the new 0 -- it becomes the new "First" waypoint
///// Thus, the first waypoint index is eliminated and all others move up a notch
if (Vector3.Distance(transform.position, targetPosition[0]) < .05)
{
targetPosition.RemoveAt(0);
}
///// Adds to list of waypoints when you "right click"
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
{
targetPosition.Add(hit.point);
}
}
}///// End Void Update
}