I have multiple spheres and I want to move them independently. I want to “left-click-mouse” to select the sphere I want to move. Then I want to “right-click-mouse” to select a location for the sphere to move to. In theory, I should (I want to…) be able to move ALL the spheres simultaneously but each to a different location. I’m not getting any errors…but I’m not getting any movement either. Any suggestions?
void Update()
{
///// I select the object here...
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Collider col = hit.collider;
if (col != null)
{
test01 = hit.collider.name;
Debug.Log (hit.collider.name);
}
}
}
///// I select the movement destination here...
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
goToHere = hit.point;
Debug.Log (hit.point);
}
}
///// I (attempt) to get the object moving here by using foreach to check through every object and move the one with correct name
foreach (GameObject gameObj in GameObject.FindObjectsOfType<GameObject>())
{
if (gameObj.name == test01)
{
transform.position = Vector3.MoveTowards (transform.position, goToHere, speed * Time.deltaTime);
}
}
}