Problem with multi prefabs collision

Edit : more informations

Hello everyone,

am still trying to make an improved version of one of my [old flash prototype][1]
and right now am in the part where the enemies should avoid each other, if you visited the link above, then you will notice that the enemies collide but they don’t remain in the same position together, and this what i don’t know how to achieve with unity, i have an enemy prefabs that have this script attached to it

using UnityEngine;

using System.Collections;

 

public class StandEnemy : MonoBehaviour

{

    

    

    private Camera      cam;                    //game camera 

    private Transform   myTransform;            //transform cache   

    

    public float speed ;

    private float currentSpeed;

    private Vector3 targetPosition;

 

    

    // Use this for initialization

    void Start ()

    {

    

        myTransform = transform;

        speed = 5;

        currentSpeed = speed;

        

        //initiating settings

        if (cam == null) {

            cam = Camera.main;

        

        }

    }

    

    // Update is called once per frame

    void Update ()

    {

        //follow the player

        float distance = myTransform.position.z - cam.transform.position.z; 

        targetPosition = new Vector3 (ShipControl.myPosition.x, ShipControl.myPosition.y, 0);

        myTransform.position = Vector3.MoveTowards (myTransform.position, targetPosition, currentSpeed * Time.deltaTime);

        

        

    }

    

    void OnTriggerEnter (Collider collider)

    {

        if (collider.gameObject.CompareTag ("stand_enemy")) {

            print ("you're touching your bro");

        }

    }

    

    void OnTriggerExit (Collider collider)

    {

        

        if (collider.gameObject.CompareTag ("stand_enemy")) {

            print ("bro is gone");

        }

    }

}

and an empty game object that work as an enemies generator that have this code attached to it :

using UnityEngine;

using System.Collections;

 

public class Enemies : MonoBehaviour

{

 

    // Use this for initialization

    

    public GameObject   StandEnemyFab;              // bullet prefab

    public Vector3 randomPos;

    public float luck;

 

    void Start ()

    {

        

        for (int i=0; i<10; i++) {

            

            randomPos.x = Random.Range (-12, 12);

            luck = Random.Range (0, 6);

            if (luck < 3) {

                randomPos.y = Random.Range (6, 8);

            } else {

                randomPos.y = Random.Range (-8, -6);

            }

            randomPos.z = transform.position.z;

            Instantiate (StandEnemyFab, randomPos, transform.rotation);

        

        }

        

    }

    

    // Update is called once per frame

    void Update ()

    {

        

        

    

    

    }

}

in the flash version the enemies were stored inside an arrayList and when a collision happens, enemiesList collide with enemiesList[i+1], then only enemiesList speed up or slow down, the other just keep his normal speed (yeah there is always an extra check) but i don’t know how to do that with c#, and also am saying that maybe there is a better way to do it, so can you guys help please ?
thank you
_*[1]: http://dox-studio.com/clickofgod.htm*_

you could actually add a second collider to the enemy, change it to trigger and work in OnTriggerEnter function in each enemies’ script.

But if you still need to store each enemy in array (if in java script) and GenericList if in (C#), Generics are better, then do this in the EnemyGenerator’s script:

JS:

  //EnemyGenerator
   #pragma strict
 
public var enemiesArray : Array = newArray()
private var newEnemy : GameObject;

newEnemy = GameObject.Instantiate(blablabla);
enemiesArray.Add(newEnemy);

later, when you need to refer from another script, you could refer to the needed cell in enemiesArray;
for example, in c#

//first establish the shortcut to script, somewhere in Start
  EnemyGenerator _EnemyGenerator = GameObject.Find("yourGeneratorGameObject").GetComponent<EnemyGenerator>()

//then, use it anywhere, including the update
 _EnemyGenerator.enemiesArray[0] would mean you want to access the first enemy. 

newEnemy is a temporaral variable, that is used and re-written, but you still store the enemies in array as the GameObjects, each one sits on it’s own cell.

for c#:

using System.Collections;
using UnityEngine;
using System.Collections.Generic;

public class EnemyGenerator{

 public List<GameObject> enemiesList = new List<GameObject>();
 private GameObject newEnemy;

 newEnemy = GameObject.Instantiate(blablabla);
 enemiesArray.Add(newEnemy);

} 

Please check this as well :slight_smile:
http://forum.unity3d.com/threads/191923-Full-corse-how-to-make-a-horror-game-AVAILABLE