Beginning Unity 3D for iOS: Part 1/3

A beginning Unity 3D tutorial that shows you how to make a simple game for the iPhone. By Christine Abernathy.

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 6 of this article. Click here to view the first page.

Get Moving: Event Functions

The game is currently way too static! It’s in dire need of movement to make it come alive.

You can add movement by creating your own scripts or using pre-packaged scripts that you attach to the relevant GameObject to perform an action. For example, in Part 1 of this tutorial, you’ll create a script that moves the player when touched. The script you create needs to respond to touch events to change the Heroic Cube’s position and rotation.

Unity supports these languages for scripting: JavaScript, C#, and Boo. In this tutorial you’ll be using JavaScript. Unity’s scripting language includes pre-defined event functions that you can use to help define the GameObject’s behavior.

A game runs at a certain amount of frames per second and one thing to keep in mind is that this rate may vary depending on the Platform on which the game is being run, and on how tied up the OS resources are by other applications. This means you shouldn’t do anything very time intensive here, and you can’t assume that it is called at any particular rate.

You’ll want to process any physics-related work inside of a FixedUpdate() function, since physics work is very time sensitive. For example, you may have a GameObject that uses physics, such as RigidBody (you’ll learn more about this later). When this object collides with another object you may want to calculate the force to apply to the object, and you should do it in a FixedUpdate() function.

For operations like changing an object’s transform position or rotation, it’s perfectly fine to do this inside of an Update() function.

  • The event function that you’ll use the most is the Update() function. If you define this function in your script, it will be called once per frame. Your game display is drawn once per frame and the Update function will therefore be responsible for the display during a particular frame.
  • Another pre-defined event function, FixedUpdate(), is called at a fixed rate independent of the frame rate. If your script defines this method it will be called at a fixed interval (in comparison to the Update method which can vary quite a bit from call to call).
  • A third useful function is Start(), called before the first frame update happens. You’ll typically put initialization operations here.
  • Finally, the Awake() function is called when the game begins. It’s another good place to put initialization code.

The Right Touch

Let’s try this out. Create a new script by selecting Assets\Create\Javascript. A new script with the default name NewBehaviorScript should show up in your Project View. Click inside the script’s name in the Project View and rename it to MoveSimple.

Double-click on the script to open it. It should open in the bundled MonoDevelop Unity Editor and should contain the following code:

#pragma strict

function Start () {

}

function Update () {

}

The Start() and Update() functions have been stubbed out for you, as they are commonly used event functions. You won’t be needing the Start() function, so remove it. Add the following variables that you’ll use to control the Heroic Cube’s movement and rotation speed below the #pragma line:

var speed : float = 3.0;
var rotateSpeed : float = 10.0;

Replace the empty Update() function with the following:

function Update () {
    // Detect mouse left clicks
    if (Input.GetMouseButtonDown(0)) {
        // Check if the GameObject is clicked by casting a
        // Ray from the main camera to the touched position.
        var ray : Ray = Camera.main.ScreenPointToRay 
                            (Input.mousePosition);
        var hit : RaycastHit;
        // Cast a ray of distance 100, and check if this
        // collider is hit.
        if (collider.Raycast (ray, hit, 100.0)) {
            // Log a debug message
            Debug.Log("Moving the target");
            // Move the target forward
            transform.Translate(Vector3.forward * speed);       
            // Rotate the target along the y-axis
            transform.Rotate(Vector3.up * rotateSpeed);
        } else {
            // Clear the debug message
            Debug.Log("");
        }
    }
}

Save your changes.

This code first looks for mouse click events, specifically from the left mouse button. A parameter of “0” signifies a left-click, “1” a right-click and “2” a middle-click when used with the GetMouseButtonDown function. If a click is detected, then the code constructs a ray (think of a laser pointer) from the Main Camera to the mouse click position.

The collider.Raycast() function detects if the collider attached to this script is hit by the ray. If the collider is hit, the collider.Raycast() call returns true. The function call takes in three arguments:

  1. The ray you constructed.
  2. A RaycastHit object, which will contain more details if the collider is hit.
  3. A ray length argument.

Assuming a true result, the follow-on code then nudges the player forward based on the speed variable, as well as rotates the player based on the rotateSpeed variable. The transform.Translate() function moves the GameObject transform in the direction and distance specified by its Vector3 input.

Vector3 is a 3D vector representation of an object that has direction and a length or magnitude. The vector can be broken down into x, y and z components.

In the code, your Heroic Cube is moved in the forward direction at a distance multiplier calculated based on the speed variable. Vector3.forward is the same as Vector3(0,0,1) and a speed multiplier of 3 will move the Cube by Vector3(0,0,3).

The direction in which the player moves is relative to the player’s local space. To get a sense of this direction, select the Cube, then the Move Tool and view the z-axis direction. That’s the direction in which the player will move.

The transform.Rotate() function rotates the GameObject transform based on an angle specified by the Vector3 input. Euler angles are used to describe the orientation of a GameObject. In the case of a rotation, the object will be moved Euler angles z degrees around the z-axis, Euler angles x degrees around the x-axis, and finally Euler angles y degrees around the y-axis.

In the code above, the player is being rotated using Vector3.up or Vector(0,1,0), which represents movement around the y-axis. The rotation angle is multiplied by rotateSpeed. The rotation is relative to the player’s local space. So your Heroic Cube will rotate around its y-axis, which, as you know, could differ from the global y-axis.

Note: A real game would never control a player using these types of movements. They are used here simply to illustrate the basic concepts involved in moving GameObjects and will be replaced with more realistic movements in the upcoming parts of the tutorial.

The code contains two Debug.Log() statements. The log output is printed in the Unity console when the game is running. You can see the last log output at the bottom of the Unity Editor. You can also open up the console by selecting Window\Console to see previous log output, as well as clear the logging.

You’ll want to keep an eye on log output at the bottom of the Unity Editor, as that’s where any script errors you have are displayed.

Now that you’ve defined a behavior via code, you can assign it to a GameObject. Select the Cube in the Hierarchy View, then select Component\Scripts\Move Simple. This step is called adding a script behavior to a GameObject and you are simply selecting the script (named MoveSimple) that you already created.

After Move Script added.

Notice that the Inspector now contains a Move Simple script section. The public variables you defined in code, speed and rotateSpeed, are visible in the Inspector View. You can now modify these public variables for just the attached GameObjects without changing the underlying script.

This means if you had two players in the scene with the MoveSimple script attached to them, you could make one player move faster than the other by increasing the speed value via the Inspector\Move Simple\Speed field.

Click Play to preview the game. Touch the player one tap at a time and pay attention to the changes in the transform position and rotation properties in the Inspector.

Game view and transform position changes.

Notice that the transform position that started at 0,1,0 has changed to 0,1,3 – in other words, there’s been a translation movement in the z direction of 3 due to the speed multiplier. The rotation has also changed from 1,1,1 to approximately 1,10,1, which closely matches the rotateSpeed multiplier.

While the game is running, you can change the speed and rotate speed variables in the Inspector. Increase the speed and watch the transform position after clicking on the player.

Stop the game and notice that the speed and rotate speed variables are reset to their original values. Any changes you make while playing the game are temporary. This allows you to tinker around with various settings and come back to the original settings when you’ve completed your preview. If you then want to make lasting changes, you can do so before testing the game again.

Note that as you play the game, the console logs the correct debug statement, depending on whether or not the player is clicked.

Christine Abernathy

Contributors

Christine Abernathy

Author

Over 300 content creators. Join our team.