Time for action – Controling the camera

This is all very interesting, except that we're fairly far away from our creation and we lack the ability to move around the scene. With traditional iOS devices we can accomplish this by accessing the accelerometer within the device:

Time for action – Controling the camera

Unity encapsulates all of the functionality of the accelerometer into the Input class and we can use its attributes and methods to connect to the accelerometer and determine what is going on with our device.

Tip

Those of you coming from Unity iPhone will notice that all of the Unity Input for iOS has been encapsulated in the Input class. There is no longer a need to use the iPhoneInput class.

  1. In the Project view create a new JavaScript script by selecting Create | JavaScript. This will create a new script named NewBehaviourScript.
  2. Double-clicking on this script will open the script in Unity's default editor Unitron. We will cover the other Unity-provided tools that can be used to develop scripts, MonoDevelop, when we deep dive into scripting.
  3. In the script window we simply enter the code for the script in the Update function:
    Time for action – Controling the camera

    Note

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

    Scripts must, generally, be attached to Game Objects to do anything useful. In our case we want to move the camera around so we need to attach this script to the camera. There are two ways to accomplish this in Unity. As with texturing, you could simply drag the script onto the camera.

  4. In the Hierarchy view select the Main Camera object.
  5. Then, in the Project view drag the script from the Project view onto either the Camera in the Inspector view or onto the object in the Hierarchy view.

    The other way to accomplish this task is to select the Main Camera in the Hierarchy view and, subsequently, in the Unity menu bar select Component | Scripts | New Behaviour Script.

    In either case you will find that Unity has added your script to the camera:

    Time for action – Controling the camera

What Just happened?

We have just examined how scripting is handled in the Unity environment. Every part of your gameplay functionality will be implemented as a script and you've just seen how simple it is to add these behaviors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.91.239