[SOLVED] Formation Collisions

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);
		}

	}

}

Fixed it. I made my Enemy prefab have a rigidbody, locked Y, with walls, also needed rigidbody, TRIGGERS DID NOT WORK for the parent / children, OnTriggerEnter2D did nothing, even at a most basic debug level. No idea why. I used several possibilities according to unity trigger / collider charts :(. I had to lock my rigidbody’s x,y,z for the walls and used the following script attached to my Enemy prefab:

using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour {

	public float formationSpeed;
	//public float formationLeftBoundary;
	//public float formationRightBoundary;

	private static string formationDirection;

	// Use this for initialization
	void Start () {

		// Initial formation direction
		formationDirection = "left";

	}
	
	// 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 OnCollisionEnter2D (Collision2D other)
	{

		print("Collision: " + other.gameObject.name);

		if (other.gameObject.name == "Left Wall") {
			formationDirection = "right";
		} else if (other.gameObject.name == "Right Wall") {
			formationDirection = "left";
		}

	}

}

I’m quite sure this is not at all efficient. But it works and is fine for the purposes of this tutorial game. Maybe next time I’ll try using an event system broadcast and make dedicated triggers spawn for the children events. Sad I couldn’t just make one collider on the parent though.

It does however automatically switch when ANY enemy hits a wall. Making it so that when an enemy is killed, the formation “width” doesn’t matter. It’s handled whenever the first enemy hits a wall and automatically adjusts. Nothing is needed on the level design part other than to lay out enemies and set a speed.