|
In the Unity Editor the main element of the rolando
character is actually a physics sphere. The animating face is a plane attached
to the physics sphere and an orthogonal camera is used so that no perspective
is present.
In this code you'll see a rolando.SendMessage(
"highlight", true ). This call sends a message to the base of the
rolando character (the physics sphere) and enables or disables its renderer.
In the actual game a white outline appears around
selected rolandos. This code showcases one method of accomplishing this task by
using the base physics element as the highlight. Thus, the physics sphere does
double duty as a gameplay mechanic and as a subtle visual cue.
There are several elements incorporated into this
code. They are as follows; tap selecting, drag-box selecting, deselecting, and
dual-touch camera dragging. The tap selection uses a simple camera ray cast to
determine if the player tapped on the rolando.
If the player did then the
rolando is instantly highlighted and selected. If the player taps elsewhere
then the rolando is deselected and the highlight is removed as well. It's
important to note the var rolando : Transform; at the top of this camera code
component.
In the Unity editor a reference is passed to the
camera containing this script. The reference is a direct link to the actual
rolando in the scene. There are countless other ways to find an object in a
scene but this is simple solution that saves CPU cycles and is an important
aspect of component based Unity development.
Even if there are multiple
character references in a scene it's no big deal to pre-plug them. Throughout
this camera script rolando refers directly to the Transform ( position and
orientation ) of the rolando character. This information can then be used to
lock the camera to a certain position and more.
Drag selecting creates a GUI box from the initial
touch position to the current touch position. Note that there are some
non-intuitive aspects to the drag selection feature. First of all, the GUI code's
y-axis is inverted from the touch or ray cast code.
The coordinate ( 0, 0 ) in
GUI code represents the upper left hand corner of the iPhone's screen while (
0, 0 ) in terms of touch or screen point ray cast represents the lower left
corner of the screen. Because of this it is sometimes necessary to subtract for
example the touch.position.y from 320 to get an appropriate GUI y-axis
position.
The drag selection is simple in spite of the
counter intuitive nature of GUI coordinates versus touch or screen coordinates.
If a rolando's 2D screen position, retrieved via
Camera.main.WorldToScreenPoint( rolando.position ) is within the GUI box formed
between the Vector2's initialSelection and touch.position then the rolando
becomes highlighted.
Finally, if the finger is lifted and the initialPosition
isn't equal to ( 0, 0 ) or the magnitude of the difference between
initialPosition and touch.position is less than five screen units then the box
check is run again. If the rolando is still contained then it becomes
highlighted and selected, otherwise it loses both highlight and selection
status.
The last part of the touch input and camera
component is the dual finger camera drag. If a rolando is not selected then the
camera is free to drag with two fingers. This is an essential part of planning
a strategy in the actual game of Rolando.
Multi-touch is such an important
feature of the hardware and it was nice that Rolando utilized it in a vital and
intuitive manner. The function LateUpdate() contains the dual touch code which
is very simple. The LateUpdate() callback is utilized in this script to create
smoother movement for the camera as it is updated after all other instructions
are completed during the current frame.
The next most important component is the rolando
script itself, which contains tilt control for a selected rolando.
private var selected : boolean;
function FixedUpdate()
{
if ( selected )
{
accel = iPhoneInput.acceleration;
rigidbody.AddForce( -accel.y * 100, 0, 0 );
}
}
function highlight( flag : boolean )
{
renderer.enabled = flag;
}
function select( flag : boolean )
{
selected = flag;
}
This small script is extraordinarily simple. The
most important feature is the FixedUpdate() callback. This is used for physics
sensitive aspects of the gameplay. The main fixture of the rolando character is
a physics sphere and code within FixedUpdate() applies a force to the physics
sphere if the rolando is selected upon tilting the device.
The iPhone hardware can
measure acceleration along three axes. In this instance the y-axis is used
which represents the top and bottom of the device when viewing the device in
its vertical orientation. Rolando is played in landscape mode, however, so it's
necessary to apply the hardware's y-axis acceleration along the game world's
x-axis.
The final script included in this simple project is
an animation script. In Rolando the characters blink from time to time. This
script showcases simple 2D animation functionality. Although Unity is a 3D
engine it is wholly possible to create a 2D game (as has been demonstrated by
numerous hits on the app store). This script only scratches the surface of 2D
animation but it's a start.
var materials : Material[];
private var blinkTimer : float;
function Update () {
if (materials.length == 0)
return;
if ( blinkTimer + 0.2 > Time.time )
renderer.sharedMaterial = materials[ 1 ];
else
renderer.sharedMaterial = materials[ 0 ];
if ( blinkTimer + 3 < Time.time )
blinkTimer = Time.time;
}
As with the other scripts any var without the
private prefix can be assigned manually through the Unity editor. In this instance
the materials or textures for the animation can be chosen via the editor. The
blinkTimer is simply a time stamp variable that determines when the character
blinks.
Well, that about wraps up this segment of Games
Demystified. Hopefully, this little journey through the tiny-big world of Rolando delivered some insight into the
iPhone hardware as well as the fun and innovative gameplay that allowed Rolando to rise to the top of the iPhone
crop this past winter.
|