Reader small image

You're reading from  Hands-On Unity Game Development - Fourth Edition

Product typeBook
Published inJan 2024
PublisherPackt
ISBN-139781835085714
Edition4th Edition
Tools
Right arrow
Authors (2):
Nicolas Alejandro Borromeo
Nicolas Alejandro Borromeo
author image
Nicolas Alejandro Borromeo

Nicolas is a Game Developer currently working as a Senior Software Development Consultant for Unity in London. He is a Unity Certified Instructor teaching Unity clients all around the globe. He started using Unity in 2008 and teaching it in 2012 in several Universities and Education Institutes.
Read more about Nicolas Alejandro Borromeo

Juan Gabriel Gomila Salas
Juan Gabriel Gomila Salas
author image
Juan Gabriel Gomila Salas

Juan graduated in mathematics with a master's degree in Teacher Training, specializing in Game Design for social casino video games and as a video game developer using Unity and Unreal Engine for both PC and mobile. He was an online teacher on Udemy from 2015 to 2022 and subsequently on his own platform Frogames Training with over 150 online courses and over 500,000 students across 130 countries.
Read more about Juan Gabriel Gomila Salas

View More author details
Right arrow

Dynamic Motion: Implementing Movement and Spawning

In the previous chapter, we learned about the basics of scripting, so now, let’s create the first script for our game. We will see the basics of how to move objects through scripting using the Transform component, which will be applied to the movement of our player with the keyboard keys, the constant movement of bullets, and other object movements. Also, we will see how to create and destroy objects during the game, such as the bullets our player and enemy shoot and the enemy waves that will be generated during the game (also called enemy spawners). These actions can be used in several other scenarios, so we will explore a few in this chapter.

In this chapter, we will examine the following scripting concepts:

  • Implementing movement
  • Implementing spawning
  • Using the new Input System

We will start by scripting components to move our character with the keyboard, and then we will make our player...

Implementing movement

Almost every object in a game moves one way or another: the player character with the keyboard, enemies through AI, bullets that simply move forward, and so on. There are several ways of moving objects in Unity, so we will start with the simplest one— that is, using the Transform component.

In this section, we will examine the following movement concepts:

  • Moving objects through Transform
  • Using input
  • Understanding Delta Time

First, we will explore how to access the Transform component in our script to drive player movement, later applying movement based on the player’s keyboard input. Finally, we will explore the concept of deltatime to make sure that movement speeds are consistent on every computer. We will start learning about the Transform API to make a simple movement script.

Moving objects through Transform

Transform is the component that holds the translation, rotation, and scale of an object, so...

Implementing spawning

We have created lots of objects in the editor that define our level, but once the game begins, and according to the player’s actions, new objects must be created to better fit the scenarios generated by player interaction. Enemies might need to appear after a while, or bullets must be created according to the player’s input; even when enemies die, there’s a chance of spawning a power-up. This means that we cannot create all the necessary objects beforehand but should create them dynamically, and that’s done through scripting.

In this section, we will examine the following spawning concepts:

  • Spawning objects
  • Timing actions
  • Destroying objects

We will start seeing the Unity Instantiate function, which allows us to create instances of Prefabs at runtime, such as when pressing a key, or in a time-based fashion, such as making our enemy spawn bullets once every so often. Also, we will learn how to destroy...

Using the new Input System

We have been using the Input class to detect the buttons and axes being pressed, and for our simple usage, that is more than enough. However, the default Unity Input System has its limitations regarding extensibility to support new input hardware and mappings.

In this section, we will explore the following concepts:

  • Installing the new Input System
  • Creating Input Mappings
  • Using mappings in scripts

Let’s start exploring how to install the new Input System.

Installing the new Input System

To start using the new Input System, it needs to be installed like any other package we have installed so far, using the Package Manager. The package is just called Input System, so go ahead and install it as usual. In this case, we are using version 1.7.0, but a newer one may be available by the time you read this chapter.

Figure 6.53: Installing the new Input System package

By default, when you install the Input...

Summary

We created our first real scripts in this chapter, which provide useful behavior. We discussed how to move a GameObject based on input and instantiate Prefabs via scripting, creating objects at will according to the game situation. Also, we saw how to schedule actions—in this case, spawning—but this can be used to schedule anything. We saw how to destroy created objects to avoid increasing the number of objects to an unmanageable level. Finally, we explored the new Input System to provide maximum flexibility to customize our game’s input. We will use these actions to create other kinds of objects, such as sounds and effects, later in this book.

Now, you are able to create any type of movement or spawning logic that your objects will need, and you can make sure those objects are destroyed when needed. You might think that all games move and create shooting systems in the same way, and while they are similar, being able to create your own movement and...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Unity Game Development - Fourth Edition
Published in: Jan 2024Publisher: PacktISBN-13: 9781835085714
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.
undefined
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 €14.99/month. Cancel anytime

Authors (2)

author image
Nicolas Alejandro Borromeo

Nicolas is a Game Developer currently working as a Senior Software Development Consultant for Unity in London. He is a Unity Certified Instructor teaching Unity clients all around the globe. He started using Unity in 2008 and teaching it in 2012 in several Universities and Education Institutes.
Read more about Nicolas Alejandro Borromeo

author image
Juan Gabriel Gomila Salas

Juan graduated in mathematics with a master's degree in Teacher Training, specializing in Game Design for social casino video games and as a video game developer using Unity and Unreal Engine for both PC and mobile. He was an online teacher on Udemy from 2015 to 2022 and subsequently on his own platform Frogames Training with over 150 online courses and over 500,000 students across 130 countries.
Read more about Juan Gabriel Gomila Salas