1. Resolving collisions step by step - If i'm making a sidescroller with ramps, i need to be able to do a lot of hit checks against the environments. I've written working code like this in other places, but I just don't get what parts of unity's environemnt to use! Should I cast four rays out from the corners of my cube? should I attach a second collider that's offset from my position and receive callbacks when that bonks into the walls? None of these solutions seem right, but obviously neither does using the built in physics because it's so fiddly and boils down to just shoving cubes.
Firstly, you don't need to calculate those collisions yourself. Just use a CharacterController or a kinematic Rigidbody. If you want to get a callback when a collision with a wall occurs, do something like this:
void OnCollisionEnter(Collision c) {
if (c.point.x > collider.center.x) {
OnHitRight(c);
} else if (c.point.x < collider.center.x) {
OnHitLeft(c);
}
}
Using the built in Collisions objects does not mean you need to use the physics system. Roll your own platformer physics.
Here is a platformer script I wrote for Unity, it has lots of dependencies so you wont be able to run it, but you can look at the logic. Check out how I see which wall it's hitting in the Act() method.
2. Dispatching events to objects after collision - When I worked in AS3, i'd have each object in a collision call a hit function on the other object and pass it's self in as an argument. This let me do type checking and special cases galore. How should I approach this in Unity? I found references for calling functions on other objects but it seems like you have to know the Class (script?) name where the function lives. Obviously i want this to be generic so I can do something like calling otherCollider.hit(this); without knowing who the other collider was.
Actually that is already a feature on Unity3d. Every object that experiences a collision will automatically have its OnCollisionEnter method fired, it receives a
Collision object with all the data you need in it. You can access the objects through the Collision object. ie Collsion.gameObject or Collision.transform.
However the exact functionality you're describing is available, although I never use it. Look at the methods SendMessage, SendMessageUpwards and BroadcastMessage (in
MonoBehaviour).
So... Something like this:
public class Bomb : MonoBehaviour {
void OnCollisionEnter(Collision c) {
// call every method Disintegrate() in all scripts attached to other object
c.gameObject.SendMessage("Disintegrate");
Explode();
}
void Explode() {
print("BOOM!");
Destroy(gameObject);
}
}
3. Control design patterns - In other environments I'd put objects into a few arrays and iterate over them every frame. Because I was manually drawing them to the screen I had to be pretty sure about the order things were happening. I haven't seen any higher level guides to control structures in unity. Should I try to put as much of the code in individual objects and just let them 'fight it out'? What options would I have if I tried to centralize control and store more information in empty game objects?
The "Update" method on each objects is a callback from the engine iterating through all instantiated GameObjects. You don't need to write an engine
in Unity because it is already a dedicated games engine. Basically the operation you're describing is already part of Unity.
In one of my first projects we had the entire game loop firing from a single Update method in one script. This is kind of double handling, and also makes your design far less flexible. There is definitely some adjusting to do, but once you get used to Unity idioms and patterns it becomes quite powerful.
Could you give me some examples of what you're trying to achieve that you don't see how to do in Unity?
edit: Don't know how to format hyperlinks apparently