I wrote this code, but it feels like it could be a bit more elegant:
Relevant code only:
GameObject newBall;
GameObject pathBall;
if (tag.Equals(pathBallTagName))
{
newBall = other.gameObject;
pathBall = gameObject;
}
else
{
newBall = gameObject;
pathBall = other.gameObject;
}
Entire .cs file:
using UnityEngine;
using System.Collections;
using BallDelegate;
public class BallCollider : MonoBehaviour {
public Spline mySpline;
private SplineController mySplineController;
public AllBallManager abm;
public string pathBallTagName = "PathBalls";
public string ballTagEnd = "Balls";
public BoxCollider front;
public BoxCollider back;
public BoxCollider spawnArea;
public string frontName = "BallBox Front";
public string backName = "BallBox Back";
public bool isInSpawn = true;
void Start() {
mySplineController = GetComponent<SplineController> ();
BoxCollider[] frontBack = GetComponentsInChildren<BoxCollider> ();
foreach (BoxCollider bc in frontBack)
if (bc != null) {
if (bc.name.Equals (frontName))
front = bc;
else if (bc != null && bc.name.Equals (backName))
back = bc;
}
}
void OnTriggerEnter(Collider other) {
if (mySplineController)
{
mySplineController.go = true;
if (mySpline)
mySplineController.gravSpline = mySplineController.currentSpline = mySpline;
}
if (other == spawnArea) {
isInSpawn = true;
}
else if (!isInSpawn) {
BallDelegateCS bd = other.gameObject.GetComponent<BallDelegateCS>();
if (bd) // only balls, no other game objects
{
GameObject newBall;
GameObject pathBall;
if (tag.Equals(pathBallTagName))
{
newBall = other.gameObject;
pathBall = gameObject;
}
else
{
newBall = gameObject;
pathBall = other.gameObject;
}
while (newBall.transform.parent)
newBall = newBall.transform.parent.gameObject; // make sure we don't get BallBall
abm.MakeWayFor(newBall, pathBall);
}
}
}
void OnTriggerExit(Collider other) {
if (other == spawnArea) {
isInSpawn = false;
}
}
}