游戏邦在:
杂志专栏:
gamerboom.com订阅到鲜果订阅到抓虾google reader订阅到有道订阅到QQ邮箱订阅到帮看

使用Unity 3D开发iOS游戏入门教程(2)

发布时间:2013-03-07 17:28:42 Tags:,,,,

作者:Christine Abernathy

这是使用Unity 3D创造iOS游戏教程系列的第二部分。

在该系列的第一部分我们通过创建一个非常简单的项目并将其用于iOS设备上而了解了Unity的基本要素。

而今天我们将添加一些函数去丰富这一项目,即包括更有趣的玩家移动和更棒的游戏场景。

让我们开始吧:场景的改变

首先打开我们在第一部分教程中所创建的Unity项目。

今天的教程将基于一个全新的场景,从而让玩家可以在之后轻松地使用之前的场景作为参考。

选择File\Save Scene as…并将新场景命名为Level_2。新的Level_2场景将出现在项目视图中。

之前场景中的所有资产(游戏邦注:包括脚本)都能够用于新场景中。在你可以改变GameObjects在Level_2场景中的呈现而不会影响到Level_1场景中相同的对象。

玩家的移动

首先我们需要强化玩家的移动。现在我们的Heroic Cube始终都是向前移动,并向右旋转。这种移动并不顺畅,也不有趣。

角色控制器是Unity的组件,我们能够将其附加到GameObject去创造更加真实的移动。我们可以基于脚本函数去操纵角色控制器。

举个例子来说吧,我们可以调用预先定义的SimpleMove函数去移动角色。在Vector3中SimpleMove函数代表执行移动的速度。角色的移动会自动算上重力,从而让它能够爬上斜坡也能爬下楼梯。

同时角色还能滑过各种障碍无需你去编写代码提示他们。

为了更好地理解这一点,我们可以在Hierarchy View中选择Cube GameObject,并选择Component\Physics\Character Controller。你将看到一个对话框在询问是否用CharacterController替换Box Collider。

replace(from raywenderlich)

replace(from raywenderlich)

点击替换。

在Inspector中你可以看到一个有关角色控制器的新组建。你需要创造一个全新的脚本去控制玩家的移动,从而让你无需再将MoveSimple脚本附加到Heroic Cube上。

move simple(from raywenderlich)

move simple(from raywenderlich)

在Inspector点击MoveSimple脚本组件右边的齿轮图标,并选择移动组件。脚本将不再出现于Inspector。

reset(from raywenderlich)

reset(from raywenderlich)

通过选择Assets\Create\JavaScript创建一个新的JavaScript资产,并将其命名为MoveAround。在MonoDevelop Editor双击打开新脚本。选择存根函数并添加如下代码:

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

function Update () {
var controller : CharacterController = GetComponent(CharacterController);

// Rotate around y – axis
transform.Rotate(0, Input.GetAxis (“Horizontal”) * rotateSpeed, 0);

// Move forward / backward
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed * Input.GetAxis (“Vertical”);
controller.SimpleMove(forward * curSpeed);
}

@script RequireComponent(CharacterController)

保存你所做出的改变。

脚本的最后一行内容明确了这一脚本只能附加到GameObject(游戏邦注:拥有一个角色控制器组件)。

Update()函数拥有角色控制器组件的句柄。然后它将围绕着y轴(基于代表着左/右移动的输入内容)进行旋转。左/右移动是默认地由左/右箭头和A/D键所控制。

Input.GetAxis()值的范围是从-1到+1。负值代表逆时针方向,而正值则代表顺时针方向。

基于来自Input.GetAxis(“Vertical”)的输入内容,SimpleMove函数能够向前或向后移动角色。这一输入内容是由向上/向下箭头或W/S键所触发的。就像水平输入一样,Input.GetAxis()值的范围是从-1到+1,负值和正值将分别触发向后和向前的移动。我们将使用倍速器去控制和旋转角度或移动距离。

将新脚本附加到Heroic Cube。

当你完成了这些设置后,你便能在玩家的Inspector上看到新的Move Around脚本组件。

character controller(from raywenderlich)

character controller(from raywenderlich)

为什么不使用Unity Editor去旋转Heroic Cube?点击游戏按键并感受左右旋转,向前并向后移动的乐趣吧!

使用左/右/上/下箭头进行移动,同时测试W/A/S/D按键。多亏了角色控制器组件和脚本,如今玩家的移动变得更加顺畅了。

使用Unity Remote进行调试

你可能会注意到,在最后的教程中基于iOS设备测试项目有点麻烦。导出到Xcode项目与在设备上创建和运行过程需要我们花费一定的时间,而这些都包含在开发过程中。

不过也存在一些更有效的方法。最快的调试方法便是使用名为Unity Remote的应用。这一应用让你能够略过多个步骤,如面向iOS创建项目,启动Xcode并在你的设备上运行。

基于Unity Remote iOS应用,我们可以将自己的iOS设备与Unity Editor连接在一起,并从iOS设备上去控制Unity Editor。这能让你在Unity Editor上快速调试游戏。

在App Store上购买Unity Remote是免费的。

注:如果要使用Unity Remote,你的iOS设备和计算机必须使用同一个Wi-Fi网络。除此之外,Unity Editor窗口还必须保持前台操作。如果你将Unity Editor窗口转向后台,你便会收到“等待游戏视图。按压‘播放’”的信息。

在Unity Editor上点击“播放”。在你的iOS设备上启动Unity Remote应用。从出现的列表中选择你的计算机。如果你的计算机并未出现在列表上,那就在Unity Remote的IP设置上输入计算机的IP地址。

list(from raywenderlich)

list(from raywenderlich)

当Unity Remote与你的计算机匹配在一起时,你的iOS设备便能够在Unity Editor上控制游戏的运行了。虽然设备显示器的分辨率不是最理想的,但这却是创建游戏原型的快速方法。当然,你也想偶尔面向iOS创建项目并通过Xcode发行,所以你可以基于更加现实的情境去测试游戏。

基于iOS测试游戏。你会发现,因为在iOS中并不存在向左/右/上/下输入,所以你不能在设备上移动玩家(游戏邦注:但是你却可以在Mac上进行控制)。接下来你将执行一些操纵杆控制去解决这一问题。

双重操纵杆,双重乐趣

幸运的是,Unity的标准资产包(包含资产)能够执行操纵杆函数。我们需要选择Assets\Import Package\Standard Assets (Mobile)从资产包中导入相关道具(如下所示)。

importing package(from raywenderlich)

importing package(from raywenderlich)

选择与操纵杆函数相关的指示道具。点击Import。在完成导入后,项目视图将包含名为Standard Assets (Mobile)的新文件夹。在带有导入操纵杆脚本的Untiy 4中你将看到如下警告:

warning(from raywenderlich)

warning(from raywenderlich)

为了解决这一问题,让我们双击警告内容。如此便会打开操纵杆脚本文件。将鼠标放在警告行上:

gameObject.active = false;

修改声明为:

gameObject.SetActive(false);

现在当你回到Unity Editor时,你会发现警告已经消失了。

首先在场景上添加Dual Joysticks预制件。预制件可以再次利用,并总是被定制为GameObject。我们可以从项目视图拖出预制件或经由脚本将其添加到场景中。例如,我们可以先创建一个代表玩家的预制件,并添加多个玩家到场景中。

在你的项目视图中打开Standard Assets (Mobile)\Prefabs文件夹。将Dual Joysticks预制件拖到层次结构视图中。

hierarchy view(from raywenderlich)

hierarchy view(from raywenderlich)

我们可以点击游戏标签并进入游戏视图看到操纵杆:

game view(from raywenderlich)

game view(from raywenderlich)

让我们回到场景视图中。

在层次结构视图中点击Dual Joysticks旁边的三角形。我们可以注意到Dual Joysticks GameObject包含了两个孩子GameObjects。

这个过程就像是“养育”过程,能够用于设置操纵杆父母/孩子GameObject之间的关系。如果你想要创造一个复合的GameObject(拥有连接GameObject),那么“养育”过程便非常有帮助。举个例子来说吧,如果你想要左右操纵杆同时发挥作用,那么当它们都被当成单一对象时,你便能够通过脚本轻松实现这一过程了。

选择LeftJoystick GameObject,并注意到它附加了操纵杆脚本组件,选择RightJoystick GameObject进行同样的核查。

操纵杆可以查出GUI Texture上的碰触事件,并基于某些限制条件去改变它的位置。例如,我们只能在特定范围内移动图像。脚本将确保位置输出的规范化,即保持在-1至+1的范围内。如此我们便能在iOS环境中使用操纵杆去替代Input.GetAxis()。

这时候你已经在场景中放置了2个操纵杆,但是它们却不能连接到Heroic Cube去触发输入内容。我们将修改脚本去分配操纵杆的任务,即右边操纵杆能够旋转玩家,而左边操纵杆将前后移动玩家。

修改MoveAround脚本去处理操纵杆的输入内容。打开脚本进行编辑,并添加两个代表移动和旋转操纵杆的公共变量:

var moveJoystick : Joystick;
var rotateJoystick : Joystick;

然后添加一个新函数,即面向操纵杆的位置并回到输出范围(在-1至+1之间)。将其添加到 Update()函数之后:

function joyStickInput (joystick : Joystick) {
var absJoyPos = Vector2 (Mathf.Abs(joystick.position.x),
Mathf.Abs(joystick.position.y));
var xDirection = (joystick.position.x > 0) ? 1 : -1;
var yDirection = (joystick.position.y > 0) ? 1 : -1;
return ( ( absJoyPos.x > absJoyPos.y) ? absJoyPos.x * xDirection : absJoyPos.y * yDirection);
}

操纵杆输入是一个Vector2输入,拥有x和y组件。我们可以使用更大的x或y绝对值去设置输出内容。我们还可以将操纵的方向作为倍增器去指示负值或正值。结果便是输出值的范围也是在-1至+1之间。

修改Update()函数去处理来自Unity Editor或iOS设备的输入内容:

function Update () {
var controller : CharacterController = GetComponent(CharacterController);

// Rotate around y – axis
var rotatePos = Input.GetAxis (“Horizontal”) ?
Input.GetAxis (“Horizontal”) : joyStickInput(rotateJoystick);
transform.Rotate(0, rotatePos * rotateSpeed, 0);

// Move forward / backward
var forward = transform.TransformDirection(Vector3.forward);
var movePos = Input.GetAxis (“Vertical”) ?
Input.GetAxis (“Vertical”) : joyStickInput(moveJoystick);
var curSpeed = speed * movePos;
controller.SimpleMove(forward * curSpeed);
}

变换的Rotate()函数将接受左/右箭头(或A/D按键)或旋转操纵杆的输入。角色控制器的SimpleMove函数将获得来自上/下箭头(或W/S按键)或左操纵杆的输入。

保存改变后的脚本。选择玩家GameObject(立方体)并注意Move Around脚本组件拥有两个关于移动操纵杆和旋转操纵杆的全新公共变量。

move around(from raywenderlich)

move around(from raywenderlich)

目前这些变量并未得到分配。我们仍需要选中玩家GameObject,并将LeftJoystick GameObject拖到Move Joystick变量。将RightJoystick GameObject拖到Rotate Joystick变量。(或者我们也可以使用每个变量旁边的选择器。)

move around(from raywenderlich)

move around(from raywenderlich)

点击Unity Editor的“播放”,并在我们的iOS设备上开始进行Unity Remote。

测试操纵杆函数并核实我们是否能使用左操纵杆向前或向后移动,并使用右操纵杆向左和向右移动。当我们在iOS设备上移动操纵杆时,我们也应该能够看到操纵杆在Unity Editor上移动。

unity editor(from raywenderlich)

unity editor(from raywenderlich)

注:也许你看到的操纵杆图像如图所示。但是不要担心,在真正的设备上出现的操纵杆并不是这样。

清晰明亮的天空盒

如今我们已经完善了Heroic Cube的移动,接下来是否该完善场景呢?到目前为止玩家一直待在一个阴暗的世界。我们应该想办法去美化游戏环境。

幸运的是这并不困难。Unity包含标准资产包中的资产,我们可以使用这些资产去创造天空,添加植被并改变地形的高度。

在这一阶段你将利用天空盒组件去添加天空。天空盒是一个较大的立方体,能够附加到游戏的Main Camera上。当你添加了组件时,你便能够添加材料去装饰它,并经过渲染而创造出天空的效果。

选择Assets\Import Package\Skyboxes开始输入过程。选择Sunny2 Skybox.mat,然后点击Import。

importing package(from raywenderlich)

importing package(from raywenderlich)

在完成输入后,你的项目视图将拥有一个全新的Standard Assets\Skyboxes文件夹,即包含最新输入的资产。

project(from raywenderlich)

project(from raywenderlich)

选择Main Camera对象,然后选择Component\Rendering\Skybox。

smooth follow(from raywenderlich)

smooth follow(from raywenderlich)

新组件将出现在Inspector。我们可以注意到新组件包含一个代表材料的变量。在此你所输入的天空盒材料便能发挥作用了。

点击Custom Skybox变量旁边小小的圆形图标。这时候将弹出Select Material对话框,其中列出了一些你的项目可以使用的材料资产。

select material(from raywenderlich)

select material(from raywenderlich)

现在场景视图将发生改变,即呈现出我们所添加的明亮天空。我们可以点击游戏标签去观看游戏视图中的天空,或编辑“播放”按键。

smooth follw(from raywenderlich)

smooth follw(from raywenderlich)

太阳也出现了!尽管有时候会出现阴天,但是我们也可以添加一些绿色植物为游戏世界增添色彩。

scene(from raywenderlich)

scene(from raywenderlich)

不要忘记植物

让我们回到场景视图。我们可以添加地形GameObject而创造植被,然后添加材料(如草地)和对象(如树木)。

这时候我们将不再需要平面GameObject,因为地形将负责铺设层面。右击层次结构视图的平面GameObject,然后选择删除。

选择Assets\Import Package\Terrain Assets。

importing package(from raywenderlich)

importing package(from raywenderlich)

点击Import去输入所有选择。这时候地形资产将出现在标准资产文件夹中。我们可以使用已经输入的资产去修改地形GameObject的行为。

选择Terrain\Create Terrain将地形GameObject添加到场景中。选择地形对象并使用Inspector将其参数域改为-1000,0,-1000。这将最大限度地延伸地形,从而提供给玩家足够广的移动空间。

terrain(from raywenderlich)

terrain(from raywenderlich)

Terrain (Script)所包含的工具能够帮助我们装饰地形:

terrain script(from raywenderlich)

terrain script(from raywenderlich)

选择绘画工具中的画笔:

paint tool(from raywenderlich)

paint tool(from raywenderlich)

我们将能用草地去装饰地形。点击Edit Textures\Add Texture:

grass(from raywenderlich)

grass(from raywenderlich)

在Add Terrain Texture对话框的纹理区域中点击“选择”,选择Grass (Hill)纹理,关闭弹出的Select Texture2D,然后选择“添加”:

terrain script(from raywenderlich)

terrain script(from raywenderlich)

点击场景然后开始绘制草地。检查原图,然后点击游戏标签:

grass scene(from raywenderlich)

grass scene(from raywenderlich)

如果只是进行景观美化便非常简单!

回到场景视图–现在我们便能够种植一些棕榈树了。在Terrain (Script)中选择“种树”工具:

terrain script(from raywenderlich)

terrain script(from raywenderlich)

点击Edit Trees\Add Tree:

add tree(from raywenderlich)

add tree(from raywenderlich)

在添加树的对话框中点击树旁边的圆形图标,选择Palm GameObject,然后点击“添加”:

terrain script(from raywenderlich)

terrain script(from raywenderlich)

将笔触大小设置为20:

brush size(from raywenderlich)

brush size(from raywenderlich)

然后我们便可以点击适当的位置去添加树了。提示:放大场景并着眼于我们想要种树的位置。在添加树的过程中尝试着调整电光源的范围和高度去浏览更多场景。

point light range(from raywenderlich)

point light range(from raywenderlich)

完成这些设置后你的场景将如下图所示:

scene textured(from raywenderlich)

scene textured(from raywenderlich)

已经很棒了!渐渐有了真实世界的雏形了。但是地形看起来还是很平坦。

这时候我们可以在地形中添加一些高度变量。在Terrain (Script)中选择最左边的工具去提高或降低地形的高度:

terrain script(from raywenderlich)

terrain script(from raywenderlich)

将笔触大小设置为20,然后在场景视图中围绕着玩家点击2至3个点(不要靠太近)以变化地形的高度。

scene textured(from raywenderlich)

scene textured(from raywenderlich)

预览游戏并在场景中移动。适当调整场景中树的数量,并记得在做出任何改变之前暂停游戏。

tree(from raywenderlich)

tree(from raywenderlich)

保存场景。

使用Unity Remote去测试项目,当你感到满意时便能够将其作为一个Xcode项目。选择File\Build Settings,在构建设置的对话框中点击Add Current去添加Level_2场景。新场景将出现在Scenes In Build列表上。这是取消Level_1场景的选定而至创建Level_2。

settings(from raywenderlich)

settings(from raywenderlich)

点击建造,必要的话选择替换之前的项目,然后启动Xcode项目。在我们的iOS设备上调配并测试。

iOS device(from raywenderlich)

iOS device(from raywenderlich)

本文为游戏邦/gamerboom.com编译,拒绝任何不保留版权的转载,如需转载请联系:游戏邦

Beginning Unity 3D for iOS: Part 2/3

By Christine Abernathy

Welcome to the second part of the tutorial series on Beginning Unity 3D for iOS!

In the first part of this series, you learned the basics of Unity by building a very simple project and deploying it to an iOS device. You’ll want to be sure to go through that part of the tutorial before moving on to this one.

Now in this second part, you’ll add functionality to enrich the project, including better player movement and better game scenery. You’ll also learn how to use Unity Remote for debugging.

Once again, game on!

Getting Started: A Change of Scenery

Open your Unity project from Part 1 of the tutorial. If you don’t have it already, here are the download links: Unity Project, Xcode Project.

You’ll make your changes for this part of the tutorial in a new scene, so you can easily have the old one for reference later if you’d like.

Select File\Save Scene as… and name your new scene Level_2. The new Level_2 scene should show up in the Project View.

All the assets you had in the previous scene, such as scripts, are now available for use in your new scene. You can make changes to the GameObjects present in the Level_2 scene without affecting the same objects in the Level_1 scene.

Gettin’ Jiggy With It

First you’ll enhance the player’s movement. Currently your Heroic Cube always moves forward and rotates right. That’s not very smooth or exciting.

A Character Controller is a Unity component that you can attach to a GameObject to help create more realistic movement. You manipulate the Character Controller through script functions.

For example, you can call a pre-defined SimpleMove function to move the character. The SimpleMove function takes in a Vector3 input representing the speed with which to perform the move. The character’s movements automatically account for gravity, allowing it to move up slopes and down stairs.

The character also slides around any obstacles it finds in its path without your having to write code to take care of that.

To see what this means, select the Cube GameObject (the player) in the Hierarchy View and select Component\Physics\Character Controller. You’ll see a dialog asking for confirmation to replace the Box Collider with a CharacterController.

Click Replace.

You should see a new component in the Inspector for the Character Controller. You’re going to create a brand-new script to control the player’s movement, so you no longer need to attach the MoveSimple script to the Heroic Cube in this scene.

Click on the gear icon to the right of the MoveSimple script component in the Inspector and select Remove Component. The script should no longer show up in the Inspector.

Create a new JavaScript asset by selecting Assets\Create\JavaScript and name it MoveAround. Double-click the new script to open it in the MonoDevelop Editor. Delete the stubbed out functions and add the following code:

Save your changes.

The last line in the script specifies that this script can only be attached to a GameObject that has a Character Controller component.

The Update() function gets a handle to the Character Controller component. It then rotates the transform around the y-axis based on inputs representing left/right movements. By default, the left/right movement is controlled by both the left/right arrows and the A/D keys.

The Input.GetAxis() value ranges from -1 to +1. A negative value will result in an anti-clockwise rotation and a positive value a clockwise rotation around the y-axis.

The SimpleMove function moves the character forward or backward, depending on the input obtained from Input.GetAxis(“Vertical”). This input is triggered by either the up/down arrows or the W/S keys. As with the horizontal input, the Input.GetAxis() value ranges from -1 to +1, and negative and positive values will result in backward and forward movement, respectively. You use a speed multiplier to control the rotation angle or the move distance.

Attach the new script to your Heroic Cube. By now you should know the drill.

(Hint: If you don’t remember, it’s Components\Scripts\Move Around. :])

When you’re done, you should see the new Move Around script component in the player’s Inspector.

Why don’t you take your Heroic Cube for a spin using the Unity Editor? Click the Play button and experience your newfound freedom – oh, the joy of being able to rotate left and right, and move forwards and backwards!

Use the left/right/up/down arrow keys to move around, and also test out the W/A/S/D keys. The player movements are much smoother now, thanks to the Character Controller component and your scripting genius.

Give yourself a pat on the back before continuing. :]

Debugging with Unity Remote

You might have noticed in the last tutorial that testing your project on your iOS device was a bit cumbersome. The entire process of exporting to an Xcode project and building and running on your device can take quite a bit of time, which really adds up during development.

The good news is there’s a better way! The fastest way to debug your game on iOS is using an app called Unity Remote. This allows you to skip the steps of building your project for iOS, launching Xcode and deploying on your device.

With the Unity Remote iOS app, you can link your iOS device to the Unity Editor and control your Unity Editor from the iOS device. This allows you to quickly debug your game inside the Unity Editor.

Unity Remote is available for free via the App Store. Download and install the app on your device.

Click Play in the Unity Editor. On your iOS device, launch the Unity Remote app. Select your computer from the list that appears. If your computer is not listed, try entering your computer’s IP address in the Unity Remote’s IP setting.

When Unity Remote has paired with your computer, your iOS device should be able to control the game running in the Unity Editor. The resolution of the display on the device won’t be ideal, but it’s a great way to quickly prototype your game. However, you’ll want to occasionally build the project for iOS and launch it from Xcode so you can properly test game physics under more realistic conditions.

Test the game on iOS. You’ll find that there’s no way to move the player on your device because your left/right/up/down inputs do not exist on iOS (however, you can still control it on your Mac). Next up, you’ll implement some joystick controls to fix this.

Double the Joysticks, Double the Fun
Lucky for you, Unity’s Standard Assets (Mobile) package includes assets that implement joystick functionality. Select Assets\Import Package\Standard Assets (Mobile) to import the relevant items (as showing below) from the package.

Select the indicated items that relate to joystick functionality. Click Import. When the import is complete, the Project View will contain a new folder called Standard Assets (Mobile). You may see a warning in Unity 4 with the imported joystick script, similar to this (in yellow at the bottom):

To resolve this, double-click on the warning. That should open the Joystick script file. Put your cursor on the line with the warning:

Modify the statement to:

Now when you switch back to the Unity Editor, the warning should vanish. Take that!

First add the Dual Joysticks prefab to your scene. A prefab is a reusable and often customized GameObject. You can add a prefab to a scene by either dragging it from the Project View or adding it programmatically via a script. For example, you could build a prefab that represents a player and programmatically add multiple players to a scene.

In your Project View, open the Standard Assets (Mobile)\Prefabs folder. Drag the Dual Joysticks prefab into your Hierarchy View.

You can see the joysticks by clicking the Game tab to go to the Game View:

Go back to the Scene View.

Click on the triangle next to the Dual Joysticks GameObject in the Hierarchy View. Notice that the Dual Joysticks GameObject is actually composed of two child GameObjects.

A procedure called parenting was used to set up the joystick parent/child GameObject relationship. Parenting is useful if you want to create a composite GameObject that has linked GameObjects. For example, you may want the left and right joysticks to be enabled or disabled at the same time, and it’s much easier to do this via script if they can be treated as a single object.

Select the LeftJoystick GameObject and note that it has the Joystick script component attached to it. Select the RightJoystick GameObject and verify the same.

The Joystick script detects touch events on the GUI Texture (the graphic) attached to it and repositions it with certain constraints. For example, the graphics only move inside a given boundary. The script also normalizes the position output so that it’s within the -1 to +1 range. This allows you to use the joystick in an iOS environment as a substitute for Input.GetAxis().

At this point you’ve placed the two joysticks in the scene, but they’re not yet connected to your Heroic Cube to drive input. You’ll modify the script to assign the right joystick the task of rotating the player, while the left joystick will move the player back and forth.

Modify the MoveAround script to handle joystick input. Open the script for editing and add two public variables representing the move and rotate joysticks (at the top where the other variables are):

Then add a new function that looks at the joystick position and returns an output between -1 and +1. Add it right after the Update() function:

The joystick input is a Vector2 input, having x and y components. You use the larger, absolute value of x or y to set the output. You use the direction of the joystick as a multiplier to denote a negative or positive value. This results in an output value between -1 and +1.

Modify the Update() function to handle input from the Unity Editor or from an iOS device:

The transform’s Rotate() function gets input from either the left/right arrow (or A/D) keys or from the rotate joystick input. The Character Controller’s SimpleMove function gets input from either the up/down arrow (or W/S) keys or from the left joystick input.

Save your script changes. Select the player GameObject (the cube) and note that the Move Around script component has two new public variables for Move Joystick and Rotate Joystick.

These variables are not currently assigned. With the player GameObject still selected, drag the LeftJoystick GameObject to the Move Joystick variable. Drag the RightJoystick GameObject to the Rotate Joystick variable. (Or instead of dragging, you can used the selector next to each variable, as indicated before.)

Click Play in Unity Editor. Start Unity Remote on your iOS device.

Test the joystick functionality and verify that you can move forward and backward using the left joystick and rotate left and right using the right joystick. As you move the joysticks on your iOS device, you should see the joysticks moving in the Unity Editor.

A Clear, Bright Skybox
Now that you’ve improved your Heroic Cube’s movements, how about improving the scenery? So far the player’s been occupying a dull, grey world. Time to do some landscaping. :]

Thankfully, this is fairly simple. Unity includes assets in the Standard Assets package that you can use to create a sky, add vegetation and vary the height of the terrain.

In this section, you’ll add a sky by means of a skybox component. The skybox is a big cube object that’s added to the Main Camera in your game. Once you add the component, you can dress it up by adding a material that covers the cube and is rendered to create the skybox effect.

Select Assets\Import Package\Skyboxes to begin the import process. Select Sunny2 Skybox.mat, then click Import.

When the import is complete, your Project View should have a new Standard Assets\Skyboxes folder that contains the newly imported asset.

Select the Main Camera object, then select Component\Rendering\Skybox.

The new component shows up in the Inspector. Notice that it has a variable representing the material. This is where the skybox material you imported earlier comes into play.

Click on the little circular icon next to the Custom Skybox variable. The Select Material dialog should pop up listing any material assets that are available for your project’s use.

Click on the Sunny2 Skybox material, then close the Select Material dialog. The Custom Skybox material should now have the Sunny2 Skybox material assigned to it.

Your Scene View will now magically change to display the sunny sky you’ve just added. You can click on the Game tab to view the sky in the Game View, or click the Play button.

The sun has come out! And even though it may appear overcast, that’s encouragement to add some green life to your world.

Don’t Forget Your Veggies
Switch back to the Scene View. You can create vegetation by first adding a terrain GameObject, then adding material (e.g., grass) and objects (e.g., trees).

You no longer need the plane GameObject, as the terrain will take over flooring responsibilities. Right-click on the plane GameObject in the Hierarchy View and select Delete.

Select Assets\Import Package\Terrain Assets.

Click Import to import all the selections. The Terrain Assets should show up under your Standard Assets folder. You can use the imported assets to modify the behavior of a terrain GameObject.

Select Terrain\Create Terrain to add a terrain GameObject to your scene. Select the terrain object and change its transform position to -1000,0,-1000 using the Inspector. This stretches the terrain out far enough to give your player room to move.

The Terrain (Script) section contains tools that you can use to dress up your terrain:

Select the Paint tool to bring up the brushes you can work with:

You’ll be painting your terrain with grass. Click Edit Textures\Add Texture:

In the Add Terrain Texture dialog, click Select in the Texture area, select the Grass (Hill) texture, close the Select Texture2D pop up, then click Add:

Click around in your scene to paint the grass. To check out your artwork, click on the Game tab:

If only landscaping could always be so easy!

Switch back to the Scene View – now you’re going to plant some palm trees! Select the Place Trees tool in the Terrain (Script) section:

Click Edit Trees\Add Tree:

In the Add Tree dialog, click the circular icon next to the Tree entry, select the Palm GameObject, then click Add:

Set the Brush Size to 20:

Start adding trees to your scene by clicking where you want the trees to be. Hint: Zoom into the scene and pan around as you place the trees. Try adjusting the Point light range and height to see more of the scene as you add trees.

When done, your scene should look something like this:

Wow, that’s much better! It’s starting to look like a real world down there. But your terrain’s still looking a bit flat, isn’t it?

You can easily add some height variation to your terrain. In the Terrain (Script) section, select the leftmost tool to raise and lower the terrain height:

Set the Brush Size to 20, then in the Scene View, click on two to three spots around the player (not too near, though) to vary the terrain height.

Preview the game and move around the scene. Tweak the number of trees in the scene as you see fit but remember to stop the game before making any changes you wish to keep.

Save your scene.

Test your project using Unity Remote and when satisfied, build it as an Xcode project. Select File\Build Settings and in the Build Settings dialog, click Add Current to add the Level_2 scene. The new scene will show up in the Scenes In Build list. Deselect the Level_1 scene to build only Level_2.

Click Build, choose to replace the previous project if asked and then launch the Xcode project. Deploy and test on your iOS device.

Not bad, eh? Imagine how long it would have taken to do all this with raw OpenGL ;]

Where to Go From Here?
Well done! You’ve taken another big stride forward in developing your Unity skills. :] Soon, your Heroic Cube will be closing in on the finish line, too.

Here are some downloads with all of the code from the project up until this point: Unity Project, Xcode Project.

In the final part of the tutorial, you’ll build on the project by adding gameplay. Then you’ll polish it up by adding all the bells and whistles that players expect.(source:raywenderlich


上一篇:

下一篇: