I’m trying to get the formation movement of left / right to work with collision or trigger instead of based on the camera viewport.
However it appears child objects don’t trigger detection against my walls. I’ve put rigidbodys and box colliders on EVERYTHING (trial and error) and tried every combination of Is trigger and using both void ontriggerenter and oncollisionenter and nothing is detected at all. This appears to be an engine limitation or expected behavior.
What is the most elegant solution to my problem. I’m posting code from EnemySpawner.cs below. The script is attached to the parent Enemy Formation object of which all position and enemy objects are under. (And yes I’ve tried colliders and rigidbodys on these as well):
using UnityEngine;
using System.Collections;
public class EnemySpawner : MonoBehaviour {
public GameObject enemyPrefab;
public float formationSpeed;
//public float formationLeftBoundary;
//public float formationRightBoundary;
private string formationDirection;
// Use this for initialization
void Start ()
{
// Initial formation direction
formationDirection = "left";
foreach (Transform child in transform) {
GameObject enemy = Instantiate (enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
enemy.transform.parent = child;
}
}
// Update is called once per frame
void Update ()
{
if (formationDirection == "left") {
transform.position = new Vector2 (transform.position.x - formationSpeed, transform.position.y);
} else if (formationDirection == "right") {
transform.position = new Vector2 (transform.position.x + formationSpeed, transform.position.y);
}
}
void OnCollisionEnter (Collision other)
{
if (other.gameObject.name == "Left Wall") {
formationDirection = "right";
} else if (other.gameObject.name == "Right Wall") {
formationDirection = "left";
}
foreach (ContactPoint c in other.contacts) {
Debug.Log(c.thisCollider.name);
Debug.Log(c.otherCollider.name);
}
}
}