Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 2017 Mobile Game Development

You're reading from   Unity 2017 Mobile Game Development Build, deploy, and monetize games for Android and iOS with Unity

Arrow left icon
Product type Paperback
Published in Nov 2017
Publisher Packt
ISBN-13 9781787288713
Length 360 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
John P. Doran John P. Doran
Author Profile Icon John P. Doran
John P. Doran
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Building Your Game FREE CHAPTER 2. Setup for Android and iOS Development 3. Mobile Input/Touch Controls 4. Resolution Independent UI 5. Advertising Using Unity Ads 6. Implementing In-App Purchases 7. Getting Social 8. Using Unity Analytics 9. Making Your Title Juicy 10. Game Build and Submission

Having the camera following our player

Currently, our camera stays in the same spot while the game is going on. This does not work very well for this game, as the player will be moving more the longer the game is going on. There are two main ways that we can move our camera. We can just move the camera and make it a child of the player, but that will not work due to the ball's rotation. Due to that, we will likely want to use a script instead. Thankfully, we can modify how our camera looks at things fairly easily, so let's go ahead and fix that next:

  1. Go to the Project window and create a new C# script called CameraBehaviour. From there, use the following code:
using UnityEngine; 

/// <summary>
/// Will adjust the camera to follow and face a target
/// </summary>
public class CameraBehaviour : MonoBehaviour
{
[Tooltip("What object should the camera be looking at")]
public Transform target;

[Tooltip("How offset will the camera be to the target")]
public Vector3 offset = new Vector3(0, 3, -6);

/// <summary>
/// Update is called once per frame
/// </summary>
void Update ()
{
// Check if target is a valid object
if (target != null)
{
// Set our position to an offset of our target
transform.position = target.position + offset;

// Change the rotation to face target
transform.LookAt(target);
}

}
}
  1. Save the script and dive back into the Unity Editor. Select the Main Camera object in the Hierarchy window. Then, go to the Inspector window and add the CameraBehaviour component to it. You may do this by dragging and dropping the script from the Project window onto the game object or by clicking on the Add Component button at the bottom of the Inspector window, typing in the name of our component, and then clicking on Enter to confirm once it is highlighted.
  1. Afterward, drag and drop the Player object from the Hierarchy window into the Target property of the script in the Inspector window:
  1. Save the scene, and play the game:

The camera now follows the player as it moves. Feel free to tweak the variables and see how it effects the look of the camera to get the feel you'd like best for the project.

You have been reading a chapter from
Unity 2017 Mobile Game Development
Published in: Nov 2017
Publisher: Packt
ISBN-13: 9781787288713
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime