- Home /
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 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[i] collide with enemiesList[i+1], then only enemiesList[i] 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
Answer by IgorAherne · Aug 14, 2013 at 07:37 PM
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 :) http://forum.unity3d.com/threads/191923-Full-corse-how-to-make-a-horror-game-AVAILABLE
thank you for replying,
you could actually add a second collider to the enemy, change it to trigger and work in OnTriggerEnter function in each enemies' script.
yes i tried that but the thing that i want to do exactly is, when 2 enemies collide to each other online one of them slow down so the other can pass or i will end up with all the enemies regrouped in the same place,
i will try your script when i get home, and by the way thank you for the link, i think it's gonna be very useful (i hope the game is in c# ) i will definitely check it when i finish my first project (simple 2d shooter) EDIT : oh you mentioned that the game is gonna be in C#, AWESO$$anonymous$$E ^_^ !! am subscribing to your channel thank you
Your answer
Follow this Question
Related Questions
PreFabs not colliding with other GameObjecs, but collide with themselves. 0 Answers
instantiate prefabs/gameObjects 1 Answer
Weird collision/physics problem. HELP,Weird physics/collision problem 1 Answer
How would I go about storing spawned prefabs to acces them ? 3 Answers
How to count collisions of the same tagged objects? 2 Answers