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 2022 Mobile Game Development

You're reading from   Unity 2022 Mobile Game Development Build and publish engaging games for Android and iOS

Arrow left icon
Product type Paperback
Published in Jun 2023
Publisher Packt
ISBN-13 9781804613726
Length 480 pages
Edition 3rd 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 (21) Chapters Close

Preface 1. Part 1: Gameplay/Development Setup
2. Chapter 1: Building Your Game FREE CHAPTER 3. Chapter 2: Project Setup for Android and iOS Development 4. Part 2: Mobile-Specific Features
5. Chapter 3: Mobile Input/Touch Controls 6. Chapter 4: Resolution-Independent UI 7. Chapter 5: Advanced Mobile UI 8. Chapter 6: Implementing In-App Purchases 9. Chapter 7: Advertising Using Unity Ads 10. Chapter 8: Integrating Social Media into Our Project 11. Part 3: Game Feel/Polish
12. Chapter 9: Keeping Players Involved with Notifications 13. Chapter 10: Using Unity Analytics 14. Chapter 11: Remote Config 15. Chapter 12: Improving Game Feel 16. Chapter 13: Building a Release Copy of Our Game 17. Chapter 14: Submitting Games to App Stores 18. Chapter 15: Augmented Reality 19. Index 20. Other Books You May Enjoy

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!

You have been reading a chapter from
Unity 2022 Mobile Game Development - Third Edition
Published in: Jun 2023
Publisher: Packt
ISBN-13: 9781804613726
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