This morning I set out to expand the last demo I created yesterday. The result is Demo4.


In Demo3, the fireballs were duplicates of the same tree of objects. In demo4, they are in a new prefab that replaces the old fireballs. This proved useful when I wanted to update the look, feel and behavior of all fireballs.
The behavior change to the fireballs is in how they handle their patrol route. Demo3 remembered their starting point and I supplied a Vector3 with the X/Y/Z coordinates they would move to. Once they reached their goal, they would compare their position with their starting point and endpoint and they would pick the one they were not at to move to next. This worked, but it was limited and it resulted in fireballs attacking walls when I duplicated my room and moved the copy 10 units to the side. Oops.
So the Demo4 version of the fireball patrol route uses waypoints for navigation. The waypoints are instances of a simple PatrolNode prefab with an attached script called DisableMeshRendererOnAwake, to show the nodes while developing, but not when playing.
The fireball patrol script exposes a collection of GameObjects and the index for the current target. Each PatrolNode added to the collection is moved to in order. I ensure constant movement speed by using Vector3.ClampMagnitude on a diff between the current and target node position and multiplying it with Time.deltaTime to scale it down based on frame render time.
Once the fireball patrol code was done, I could easily add bigger patrol routes, so I did and most fireballs now chase after each other in the room.
The number of rooms I increased one-by-one until there were four.
Room one is the original room of Demo3, but the fireballs patrol in a square instead of up and down a line. There’s a corridor to room two, which is identical to room one except that there are no spotlights. The fireballs are stronger point light sources now, so they illuminate the room rather well. Next is a connection to room 3, which has no more inner wall and no spotlights and has each fireball patrol around its own pillar. Next is a connection to room four.
Room four is 20×20 instead of the 10×10 of the other rooms, but it has a 10×10 area in the center that’s a copy of room three except that there are no walls. The walls are half height and placed at the edges. Instead the floor is covered in grass and most of the room looks up to a skybox.
Since all solid objects are basically cubes that have been stretched to non-cube shapes, I’m making a new Material for each aspect ratio of wall or pillar I need. I’m sure there’s a better way to do it. I could look at turning one big 20×0.1×3 wall into a collection of 0.1×0.1×0.1 cubes and stacking them. Since the texture is seamless, this should work well, but there’d be a lot of different objects and lots of textures on sides you can’t see. It seems rather inefficient. The alternative is to figure out how I can create a texture that fits my wall’s dimensions exactly.

Tonight, I created Demo5 to get a better grasp on collisions and the physics model. It’s a simple bowling game built on top of the roof of Demo4 🙂
The player holds (via a FixedJoint) a heavy sphere, the ball. The BreakVelocity and BreakTorque are set to infinite, so the ball will stay glued to the player.
The player has an Update function that checks Input.GetButton(“Fire1”) to see if the left mouse button or Ctrl is pressed. If so, it starts adding Time.deltaTime to a counter and the ball is forced to face the same way as the player. Once the button is released, the square root of the counter value is multiplied with an arbitrary value of 300 and applied as forward force on the ball. The ball’s BreakVelocity and BreakTorque are set to 0, so the ball immediately snaps loose and is propelled in the direction the player is looking.
The pins are instances of a simple cylinder prefab. They have a script that checks if their rotation is still (0,0,0). If any of the dimensions change more than 0.5 degrees, assume the pin has fallen and marke it as not standing.
There’s a GUIText element in the top left corner. Because all pins have been tagged with ‘Pin’, I can simply use GameObject.FindGameObjectsWithTag(“Pin”) to find all pins and check if they are still standing. Count the standing ones and display it in the GUIText.
The thing that took me longest to figure out was which collision detection method to use for these objects. I read through the docs a bit and it looked like ContinuousDynamic was the best option, as it checked against just about everything. Pins and ball used it, but the ball just bounced off the pin without making it move a hair. I lifted the ball in the air above the pin to drop it, and it did push the pin over. After an hour of fiddling and trying to write custom OnCollisionEnter() handlers I started flippiing the collision detection method and found that Continuous did what I wanted without requiring scripts. Lesson learned.
Fiddling with mass and drag taught me that a ball does not necessarily have to keep rolling forever.
The player is still set to be Kinematic, so it is not affected by physics at all. This also seems to mean that it does not make a pin fall over when the player pushes against the pin. Disabling Kinematic turns the standard FPS Controller’s jump action into a jump that keeps going on forever. Not quite what I intended.
Random lessons learned
- Prefabs are very useful to base repeatable objects on, it makes it so much easier to change them all later on.
- Using empty GameObjects is a very useful method for grouping objects.
- Using objects as nodes for defining patrol routes beats manually typing in coordinates: they move along when duplicating a room.
- Abusing cubes to create walls is easy to get results, but it’s not the prettiest solution when using a one-sided texture that’s repeated on each side of the ‘cube’.
- The physics system in Unity3D is nice, but it takes a while to grasp the subtleties.
And that’s it for today. Perhaps I’ll create more demos during the week at night. Otherwise next weekend is likely going to be another hackfest with Gerard. He’s making progress on his demos, (though I need to bug him about posting his demos here) so it’s not going to be long before we’ll start working on tech demos/tools that can be useful for our first real game.