Welcome to the second part of the Unity3d for iOS series Tutorial!
In the first part of this series, you've learned some of the basic skills of unity to build a very simple project and deploy it to iOS devices. Before continuing this tutorial, you should confirm that you have read the first section.
Now in the second part, you have to add some features to enhance our project, which include better character movement, and a better game background. You'll also learn how to use unity remote to debug.
The game opens again!
Let's get started: Change the background
Open your Unity project in the first part of the tutorial. If you are not ready, here is the link to download: Unity project, Xcode project.
In this part of the tutorial, you have to make changes in a new scene, so you can easily open the old one to reference if you like.
Choose FileSave Scene as ... and name your new scene for level_2. This new level_2 scene is added to the project view.
All the resources you used in the previous scenario, such as scripts, can be used in your new scene. You can change the gameobject in the level_2 scene without affecting the same objects in the Level_1.
Well, enjoy it.
First you need to enhance your character's mobility. At present, your heroic little square will just rotate forward and to the right. That kind of movement is not very smooth and interesting. The
Character Controller is a unity component that you can connect to Gameobject, which helps you create more realistic motion. You can control the character controller with a script function.
For example, you can call a pre-defined simplemove function to move a character. The Simplemove function uses a Vector3 as input to represent the speed at which the movement is acting. The movement of a character is automatically calculated based on gravity, allowing it to climb up and down stairs. The
role bypasses any obstructions, and it finds its path without requiring you to write code to focus on it.
To see what this means, select the Block Gameobject (your character) in the hierarchy view and select "Componentphysicscharacter Controller." You will see a dialog box to confirm that you are using character controller instead of box Collider
Click "Replace"
in Inspector you will see a charactor The new component of the controller. You will be creating a new script to control the movement of the player, so you no longer need to bind the Movesimple script to your heroic little squares in this scene.
in inspector, click the gear icon in the upper-right corner of the Movesimple script and select Remove component. This script will not appear in the inspector.
Select Assetscreatejavascript to create a new javacript resource, and then name it movearound. Double-click the new script to open it in MonoDevelop. Delete the automatically generated function, and then add the following code:
var speed:float = 3.0; var rotatespeed:float = 3.0;function Update () {var controller:charactercontroller = getcomponent (Charactercontrol LER); Rotate around Y-axis transform. Rotate (0, Input.getaxis ("horizontal") * rotatespeed, 0); //Move forward/backward var Forward:vector3 = t Ransform. Transformdirection (Vector3.forward); var curspeed:float = speed * Input.getaxis ("Vertical"); Controller. Simplemove (Forward * curspeed);} @script requirecomponent (charactercontroller) |
Save your changes.
The last line in this script specifies that the script can only be connected to Gameobject with the character controller component.
This update () function gets a handle to a character controller component. Make the rotation transform around the y axis, based on the input that represents the left and right movement. By default, left and right arrows and A/D keys are controlled.
The value range for Input.getaxis () is 1 to +1. Around the y-axis, negative values produce a counterclockwise rotation, and positive values produce a clockwise rotation.
The Simplemove function moves the character North Korea before or after it, based on the input obtained from the Input.getaxis ("Vertical"). This input is triggered by a front or back arrow or W/S key. As with the horizontal input, the value range of the Input.getaxis () ranges from 1 to +1, and negative and positive values represent backward and forward movements, respectively. You use a speed factor to control the angle of rotation and the distance of movement.
Associate a new script on a heroic little square. Now you know how to do it. (Hint: If you don't remember, this is Componentsscriptsmove Around.:]) When you're done, in the role of inspector, you should see a new Move Around Script component.
Why not use the Unity editor to try and spin your heroic little squares? Click the Play button and then experience your newly created mobile effect – oh, experience the joy of turning around, moving forward and backward.
Use the Left/right/up/down button to turn around, and then test the W/A/S/D key. The player is moving more smoothly now, thanks to the Character controller component and the gene you are scripting.
Before you go on, pat your back and take a break. :]
Debug with Unity remote
In the last tutorial, you may have noticed that testing your project on your iOS device is a bit of a hassle. Exporting to a Xcode project, the entire process of building and running on your device takes some time, and the time actually increases to the development process.
The good news is that there's a better way! The quickest way to debug your game on iOS is to use an app called Unity remote. This lets you skip these steps for building a project for your iOS, launching Xcode and then deploying it on your device,
With the Unity Remote iOS app, you can link your iOS to Unity Editor and then control your unity editor from iOS devices. This allows you to quickly debug your game in the Unity editor.
Unity remote can be obtained free of charge through AppStore. Download and install the app on your device.
Note: To use unityremote, you need your iOS device and your PC should be on the same Wi-Fi network. Also, in order for this to work, the Unity Editor window must be in the foreground. If you put the Unity Editor window in the background, you'll get "waiting for Game View" in the iOS app. Press ' Play ' message.
Click Play in the Unity editor. On your iOS device, run the Unity Remote app. From the list that appears, select your computer. If your computer isn't in the list, try entering your computer's IP address in the Unity remote's IP settings.
When Unity remote is already paired with your computer, your iOS device should be able to control the game running in Unity Editor. The resolution displayed on the device will not be ideal, but this is the best way to quickly show your game. However, you should also build your project for iOS occasionally and load the game from Xcode so that you can accurately test the physical effects of the game under more realistic conditions.
Test this game on iOS. You will find that there is no way to move your player on your device, because your left/right/up/down button does not exist on iOS (although you can still control it on your Mac). Below, you will implement a joystick function to correct this.
Double the swing, double the joy.
Fortunately for you, Unity's standard repository includes resources to implement the joystick feature. Select Assetsimport packagestandard Assets (Mobile) to import the relevant entries from the package (as seen below).
Select the specified item associated with the joystick feature. Click Import. When the import process is complete, the project panel will contain a new folder called Standard Assets (Mobile). In Unity4, you can see the script with the imported joystick with a warning, just like this (the yellow part at the bottom)
To resolve this issue, double-click on the warning. This will open the joystick's script file. Put your cursor on the line with the warning:
Gameobject.active = false; |
Modify this statement to:
Gameobject.setactive (FALSE); |
Now when you switch to Unity Editor, this warning should not exist. I understand!
First add a double stick preform to your scene. Precast parts are reusable and are usually gameobject that need to be customized. You can drag it out from the project panel, add a prefab to your scene, or add it programmatically by script. For example, how do you construct a prefab to represent a role, and then programmatically add multiple characters to a scene.
In your project panel, open the standard Assets (Mobile) prefabs folder. Drag the Dual joysticks prefab into your hierarchy view.
Click Game View, and you can see the joystick in the Games view:
Back to Scene view
Click the triangle on the dual joysticks Gameobject side of the hierarchy view. Note that dual joysticks Gameobject actually consists of two sub-gameobject.
This process is called paternity, which is used to establish a rocker-parent-child gameobject relationship. Paternity is very useful, you can create a composite gameobject, this composite gameobject contains other gameobject. For example, you can make the left and right sticks work or fail at the same time, and if they can be seen as a simple object, it's quite easy to do this with a script.
Select Leftjoystick Gameobject, and note that there is a joystick script component associated with it. Select the Rightjoystick gameobject to see if it is the same.
The Joystick script detects touch events attached to the GUI texture on top of it, obtaining positioning under specific constraints. For example, a graph moves only within a given range. The script also normalized the output of the location, allowing it to be between 1 and 1. This allows you to use the joystick as an alternative to Input.getaxis () in an iOS environment.
By this step, you've placed two sticks in the scene, but they're not connected to your heroic little square to drive the input. You're going to change the script to assign the right joystick to rotate the character's task while the left joystick moves your character back and forth.
Modify the Movearound script to handle joystick input. Open a script to edit and add public variables to represent moving and rotating sticks (above other variables):
var movejoystick:joystick;var rotatejoystick:joystick; |
Then add a new function to view the position of the joystick and return the output from 1 to + 1. is added after the upate () function.
function Joystickinput (joystick:joystick) { var absjoypos = Vector2 (mathf.abs), 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);} |
The joystick input is a Vector2 with x and Y components. You use the larger of the absolute value of x or Y as the output. You use the direction of the joystick as a multiplier to represent a negative or positive value. The result of the output value is between 1 and + 1.
Modify the update () function to handle input from a unity editor or an iOS device:
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);} |
The rotate () function of this transform obtains input from the input of the left/right (or A/d) key or the rotary joystick. The Simplemove function of the role controller is entered from the UP/down arrow (or W/S) key or the input of the left joystick.
Save your script's changes. Select the character gameobject (this block), and note that the Movearound Script component has two new public variables for the move stick and rotation stick.
These variables are not currently assigned. Keep the character Gameobject still selected, drag the left joystick gameobject to the shift bar variable. Drag the right joystick gameobject to the rotation rocker variable. (or instead of dragging, you can use the selector on the edge of each variable, as noted previously)
Click Player in the Unity editor. Turn on Unity Remote on your iOS device.
Test the function of the joystick, and then make sure you can use the left joystick to move back and forth, using the right joystick to rotate around. When you move the stick on your iOS device, you can see the joystick moving in the Unity editor.
Note: The picture of the joystick you see here may be stretched. Don't worry, they won't show up like this on the actual device.
A clear sky.
Now that you've improved the movement of your heroic little squares, how about improving the background? So far, the characters are in a dull and gloomy world, and it's time to beautify them.:]
Thankfully, it's very simple. Unity includes this resource in a standard resource bundle that allows you to create the sky, adding vegetation and different heights of terrain.
In this section, you will add a sky with the skybox component. Skybox is a large block object that is added to the main camera of your game. When you add this component, you can create a sky effect by adding the material that covers the block, putting clothes on the block, and being rendered.
Select Assetsimport packageskyboxes to start the import process. Select Sunny2 Skybox.mat, and then click Import.
When the import is complete, your project view should have a new standard Assetsskyboxes folder that includes a new imported resource.
Select the main camera object and select Componentrenderingskybox
This new component appears in the Inspector. Note that it has a variable to represent the material. Here is where you have previously imported the skybox material to play a role.
Click the Small circle icon on the side of this custom Skybox variable. A dialog box for selecting a material will pop up to list all the available materials for your project.
Click Sunny2 skybox Material, and then close the Select Material dialog box. This custom Skybox variable should now have a Sunny2 skybox material to pay for it.
Your scene will magically change to show the clear sky you just added, you can click on the game section to display the sky in the game view, or click the Play button.
The Sun comes out! Although it looks gloomy, it will encourage you to add some green life to your world.
Don't forget to add green
Switch back to the scene view. You can create vegetation by adding a terrain gameobject, and then add some materials (for example, grass) and some objects (for example, trees).
You no longer need a planar (plane) Gameobject, and terrain (terrain) takes the place of the ground. In the hierarchy view, right-click Plane Gameobject, and then select Delete.
Select Assetsimport packageterrain Assets.
Click Import to export all of the selected content. The terrain resource should appear under your standard resource folder. You can use this imported resource to change the behavior of the terrain gameobject.
Select the Terraincreate terrain to add a terrain gameobject to your scene. Select the terrain object and use inspector to change its position to -1000,0,-1000. This step stretches the terrain to large enough to give your character enough space to move.
The terrain (script) bar includes tools that let you decorate your terrain:
Choose the paint tool to bring the brushes you can work with:
You are going to use grass to plot your terrain. Click Edit Texturesadd Texture:
In the Add Terrain Texture dialog box, click Select in the Texture area, select grass (Hill) texture, close the Select Texture2d Pop-up, and then click Add:
Click in the view of your scene to draw the grass. To check your authoring results, click on the game section:
It's always so easy to beautify!
Switch back to Scene view – now you're going to draw some palm trees! In the terrain (script) bar, choose the Place Trees tool:
Click Edit Treesadd Tree:
In the Add Tree dialog box, click the Circle icon on the edge of the tree input box, select palm tree Game Object, and then click Add:
Set the Brush size to 20:
Start adding trees to your scene by clicking where you want to add the trees. Tip: When you place trees, zoom in on the scene and move the scene. When you add trees, try to adjust the range and height of the point light to see more scenes.
After the end, your scene should look like this:
Wow, pretty good! It starts to look like a real world out there. But your terrain is still a bit flat, isn't it?
You can easily add some height changes to your terrain. In the terrain (script) bar, select the leftmost tool to increase and decrease the height of the terrain:
Set the brush size to 20, and then in the scene's attempt, click the two or three points around the player (not too close,) to change the height of the terrain.
Preview the game and walk around in the scene. Fine-tune the number of trees in the scene until it looks right, but remember to stop the game before you want to keep any changes.
Save your scene
Test your project with unity remote until you're satisfied and build it in the Xcode project. Select Filebuild Settings, and then in the Build Settings dialog box, click Add Current to add the level_2 scene. This new scene in the scene will appear in the build list. Do not select the Level_1 scene, just build level_2.
If you are asked to click Build, select Replace the previous item, and then load the Xcode project. Deploy and test on your iOS device.
Isn't that nice, huh? Imagine how long it will take to complete these if you're still using OpenGL:]
Where are you headed?
That's good work! You're taking a big step forward in expanding your unity skills. Your heroic little squares will also be closer to the finish line.
Here are some of the code downloads from the beginning of the project to the present: Unity Project, Xcode project.
In the final part of this tutorial, you will add game elements to your project. Add color to your game by adding the bells and whistles your players expect.
Getting Started with IOS Unity3d game engine ②