Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Unity 2022 Mobile Game Development
  • Table Of Contents Toc
Unity 2022 Mobile Game Development

Unity 2022 Mobile Game Development - Third Edition

By : John P. Doran
5 (10)
close
close
Unity 2022 Mobile Game Development

Unity 2022 Mobile Game Development

5 (10)
By: John P. Doran

Overview of this book

Unity is a well-established player in the mobile game development sphere, and its new release, Unity 2022, is packed with new, exciting features. In Unity 2022 Mobile Game Development, Third Edition, you'll get to grips with the Unity game engine by building a mobile game and publishing it on the most popular mobile app stores as well as exploring the all-new features. This book provides a comprehensive and practical approach to mobile game development, helping you build an endless runner game. Starting with setting up a simple Unity project for mobile development, you’ll delve into various essential aspects needed to successfully create and publish your game. You’ll acquire a range of skills, such as incorporating touch gestures, monetizing your game with Unity Ads and in-app purchases, designing an intuitive UI, and seamlessly integrating social media functionalities. Additionally, you’ll gain valuable insights into player preferences and behavior using Unity's analytics tools. You’ll also explore features of augmented reality in Unity 2022, enhancing your game's appeal. By the end of this book, you’ll be well-equipped to reap the power of Unity 2022 to build, optimize, and publish robust cross-platform mobile games with C#, as well as widening your skill set and enhancing your credentials as a game developer.
Table of Contents (21 chapters)
close
close
1
Part 1: Gameplay/Development Setup
4
Part 2: Mobile-Specific Features
11
Part 3: Game Feel/Polish

Update function versus FixedUpdate function

The next thing to look at is our movement code. You may have noticed that we are currently using the Update function in order to move our player. As the comment above it states, the Update function is called once per frame that the game is running. One thing to consider is that the frequency of Update being called is variable, meaning that it can change over time. This is dependent on a number of factors, including the hardware that is being used. This means that the more times the Update function is called, the better the computer is. We want a consistent experience for all of our players, and one of the ways that we can do that is by using the FixedUpdate function.

FixedUpdate is similar to Update with some key differences. The first is that it is called at fixed timesteps, meaning the same time between calls. It’s also important to note that physics calculations are done after FixedUpdate is called. This means code-modifying physics-based objects should be executed within the FixedUpdate function generally, apart from one-off events such as jumping:

/// <summary>
/// FixedUpdate is a prime place to put physics
/// calculations happening over a period of time.
/// </summary>
void FixedUpdate()
{
    // Check if we're moving to the side
    var horizontalSpeed = Input.GetAxis("Horizontal") *
                           dodgeSpeed;
    rb.AddForce(horizontalSpeed, 0, rollSpeed);
}

By adjusting the code to use FixedUpdate, the ball should be much more consistent in its movement speed.

Note

For more information on FixedUpdate, check out https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html.

Putting it all together

With all of the stuff we’ve been talking about, we can now have the final version of the script, which looks like the following:

using UnityEngine;
/// <summary>
/// Responsible for moving the player automatically and
/// receiving input.
/// </summary>
[RequireComponent(typeof(Rigidbody))]
public class PlayerBehaviour : MonoBehaviour
{
    /// <summary>
    /// A reference to the Rigidbody component
    /// </summary>
    private Rigidbody rb;
    [Tooltip("How fast the ball moves left/right")]
    public float dodgeSpeed = 5;
    [Tooltip("How fast the ball moves
        forward  automatically")]
    [Range(0, 10)]
    public float rollSpeed = 5;
    // Start is called before the first frame update
    public void Start()
    {
        // Get access to our Rigidbody component
        rb = GetComponent<Rigidbody>();
    }
    /// <summary>
    /// FixedUpdate is a prime place to put physics
    /// calculations happening over a period of time.
    /// </summary>
    void FixedUpdate()
    {
        // Check if we're moving to the side
        var horizontalSpeed = Input.GetAxis("Horizontal") *
                              dodgeSpeed;
        rb.AddForce(horizontalSpeed, 0, rollSpeed);
    }
}

I hope that you also agree that this makes the code easier to understand and better to work with. Now, we can move on to additional features in the game!

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Unity 2022 Mobile Game Development
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon