Wednesday, April 25, 2012

Flight Control Overview

I'm trying to get my prototype hosted on the web but have ran into some problems. So I will cover the flight control script. It's a rather long script and involved script that uses the player data and vehicle data scripts.

This script was originally from the Space Game Starter Toolkit created by ArenMook. I have heavily modified the code to fit the game. It is also unrecognizeable from its original format.
It starts out with the usual declarations and documentation:

At this point, I will point out that the variables in vehicle data are replicated here. There is a very specific reason for this. First, I plan on leaving vehicle data (and player data) relatively untouched by the mission. These two data structures will represent the benchmark values for a fighter and do figure into calculations. Second, declaring them as values allow me to directly manipulate them in this script without worrying about unforseen interactions.

The variable declarations are pretty run of the mill; however, we do use a couple of new declarations. We set up a Listener for collisions, a List for collisions and make a public declaration for a GameObject.

The collision stuff is a hold over from the original code, and the game object declarations allow us to create (or instantiate) an explosion when the fighter is destroyed during gameplay.

The main section has many of the same type of statements in the data scripts. We do create a reference to the vehicle data script here. vInitialData = VehicleData.Find(vTrans) creates a reference to the VehicleData script attached to the fighter. We don't change any values; we use it as a quicker way of accessing the "benchmark" data.

At this point, there are calculation scripts. Update() allows the user to control the ship (if it is own ship) and is a built in Unity script. The LateUpdate() and FixedUpdate() are also built in scripts. Update() executes every frame. As long as your computer is not taxed, it is the quickest update script. LateUpdate() only updates after the other update functions have completed execution, and FixedUpdate() updates at a fixed interval. In the prototype, FixedUpdate() updates every 0.1 seconds.

The UpdateInput() is not a built in fuction; it is a user defined function. I deleted the android app section for readability. Basically, this function checks the mouse position against center screen and generates a turn force based on the offset. In addition, it checks for inputs for right, up and forward momentum (left appears as a negative right and down appears as a negative up). All of these inputs are placed parsed and placed into the manuevering variables (vTurn, vMove and vSmoothMove).

The future AI routines will directly place values into vTurn, vMove and vSmoothMove via the defined methods. The FixedUpdate() function actually executes manuevering based on what is in these variables by UpdateSteering() and UpdateTorque().

UpdateSteering() calculates the direaction and amount of force to apply to the craft. Based on what was calculated into the vector variables for vMove and vSmoothMove, it will apply the appropriate force to the fighter.
UpdateTorque() handles the actual turning of the fighter. It gets the values stored in vTurn and calculates what kind of angular velocity to apply to the fighter to make it turn.

DamageNavigation() applies damage to the ship's structure and navigation systems. It is invoked in a special script called DamageControl. The DamageControl script calculates the amount of force that impacted the ship and passes that value here via the DamageNavigation() script. The ApplyDamage() script does the actual calculations, and the DestroyShip() function will destroy the ship (and create a nice explosion) if the structure falls below zero.

These two scripts are "flags" used by AI routines. Basically, it just tells the AI routine if the ship's navigation systems are offline due to damage.
This actually a pretty long script.