Camera setup and controls

Select Main Camera in Scene. We need to set it up and add a script to have it follow the character.

How to do it...

  1. With MainCamera selected in Scene, move to the Inspector panel and access the Camera settings. Be sure that the Field Of Vision, Far, and Near clipping planes are set as shown in the following screenshot:
    How to do it...
  2. Now create a new C# script in the Scripts folder, name it Camera Control and open it in Monodevelop.
  3. In the script, we set a reference to the character and in the Update() function we align the camera position with that of the character.

    What follows is the code for the script:

    using UnityEngine;
    using System.Collections;
    
    public class CameraControl : MonoBehaviour {
    
      public int distance;
      private Transform follow;
    
      // Use this for initialization
      void Start () {
    
        follow = GameObject.Find("runner").GetComponent<Transform>();
      }
      
      // Update is called once per frame
      void Update () {
    
        Vector3 v = new Vector3 (follow.position.x, follow.position.y, distance);
        transform.position = v;
      }
    }
  4. Next, we save the script and move back to Unity. Drag the script from the Project panel onto the Main Camera in the scene.
  5. Set the Distance public variable in Inspector to a value you like. We picked a value of -15 to begin with. Check out the following screenshot that illustrates this:
    How to do it...

How it works...

This camera simply keeps the same character x and y, so the character itself is always at the center of the camera focus.

Please take into consideration that this script could be improved, for example, by allowing the player to look a bit ahead of the character before taking a leap. To delve more into the properties of Cameras in Unity, you can refer to the manual at http://docs.unity3d.com/Manual/class-Camera.html.

There's more...

In case you didn't do it yet, we recommend you add a Directional light to your scene so materials will look better on screen. You should know how to do that: from the top menu, navigate to GameObject | Create Other | Directional Light, as shown in the following screenshot:

There's more...

Now you can rotate the directional light using the gizmo as you see fit for your taste. Being a Directional Light, it will cast its rays independently from its actual position and distance from the character.

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

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