hi!
i want to let collided gameobjects follow the player, but in different distances. so the first collided should follow the player with a distance of 10, the second with a distance of 11, the third with a distance of 12 and so on. in the game it is not clear which gameobject the player collides first, so i make a array that it will order the objects in the order the player collided them. now i want to reach the first object in the array through the script to tell it to follow the player with a distance of 10, then i want to reach the second object in the array to tell it to follow the player with a distance of 11 and so on.
here is my script so far. it is attached to the objects the player will collide with. but it does not work, it says in the console: ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
could anybody please help me to achieve the right solution for the above described?
thank you so much!
var collided : Transform;
var player : Transform;
var foundObjects = new Array();
function OnCollisionEnter (hit : Collision)
{
if(hit.gameObject.tag == “Player”)
{
foundObjects.Add(gameObject);
}
}
function Update ()
{
var speed : float = 10;
var distance : float = Vector3.Distance(collided.transform.position, player.transform.position);
var firstFound = foundObjects[0];
var secondFound = foundObjects[1];
var thirdFound = foundObjects[2];
for (firstFound in foundObjects)
{
if (distance > 10)
{
collided.LookAt(player);
collided.Translate(speed*Vector3.forward*Time.deltaTime);
}
else
{
speed = 0;
}
}
for (secoundFound in foundObjects)
{
if (distance > 11)
{
collided.LookAt(player);
collided.Translate(speed*Vector3.forward*Time.deltaTime);
}
else
{
speed = 0;
}
}
for (secoundFound in foundObjects)
{
if (distance > 12)
{
collided.LookAt(player);
collided.Translate(speed*Vector3.forward*Time.deltaTime);
}
else
{
speed = 0;
}
}
}