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*_