Reader small image

You're reading from  Learning C# by Developing Games with Unity - Seventh Edition

Product typeBook
Published inNov 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781837636877
Edition7th Edition
Languages
Tools
Right arrow
Author (1)
Harrison Ferrone
Harrison Ferrone
author image
Harrison Ferrone

Harrison Ferrone is an instructional content creator for LinkedIn Learning and Pluralsight, tech editor for the Ray Wenderlich website, and used to write technical documentation on the Mixed Reality team at Microsoft. He is a graduate of the University of Colorado at Boulder and Columbia College, Chicago. After a few years as an iOS developer at small start-ups, and one Fortune 500 company, he fell into a teaching career and never looked back.
Read more about Harrison Ferrone

Right arrow

Movement, Camera Controls, and Collisions

One of the first things a player does when starting a new game is to try out character movement (if, of course, the game has a moveable character) and camera controls. Not only is this exciting, but it lets your player know what kind of gameplay they can expect. The character in Hero Born will be a capsule object that can be moved and rotated using the W, A, S, D, and arrow keys, respectively.

We’ll start by learning how to manipulate the player object’s Transform component and then replicate the same player control scheme using applied force. This produces a more realistic movement effect. When we move the player, the camera will follow along from a position that is slightly behind and above the player, making aiming easier when we implement the shooting mechanic. Finally, we’ll explore how collisions and physical interactions are handled by Unity’s physics system by working with our item pickup Prefab.

...

Managing player movement

When you’re deciding on how best to move your player character around your virtual world, consider what’s going to look the most realistic and not run your game into the ground with expensive computations. This is somewhat of a trade-off in most cases, and Unity is no different.

The three most common ways of moving a GameObject and their results are as follows:

  • Option A: Use a GameObject's Transform component for movement and rotation. This is the easiest solution and the one we’ll be working with first.
  • Option B: Use real-world physics by attaching a Rigidbody component to a GameObject and apply force in code. Rigidbody components add simulated real-world physics to any GameObject they are attached to. This solution relies on Unity’s physics system to do the heavy lifting, delivering a far more realistic effect. We’ll update our code to use this approach later on in this chapter to get a feel for...

Moving the player with the Transform component

We want a third-person adventure setup for Hero Born, so we’ll start with a capsule that can be controlled with keyboard input and a camera to follow the capsule as it moves. Even though these two GameObjects will work together in the game, we’ll keep them, and their scripts, separate for better control.

Before we can do any scripting, you’ll need to add a player capsule to the scene, which is your next task.

We can create a nice player capsule in just a few steps:

  1. Click on + > 3D Object > Capsule from the Hierarchy panel and name it Player.
  2. Position the player capsule in the level wherever you like, but I like to put it close to one of the central ramps. It’s also important to position the capsule above the ground plane, so be sure to set the Transform Y position value to 1 in the Inspector.
  3. Select the Player GameObject and click on Add Component at the bottom of the Inspector...

Scripting camera behavior

The easiest way to get one GameObject to follow another is to make one of them a child of the other. When an object is a child of another, the child object’s position and rotation are relative to the parent. This means that any child object will move and rotate with the parent object.

However, this approach means that any kind of movement or rotation that happens to the player capsule also affects the camera (like a waterfall affects the water downstream), which is something we don’t necessarily want. We always want the camera to be positioned a set distance behind our player and always rotate to look at it, no matter what. Luckily, we can easily set the position and rotation of the camera relative to the capsule with methods from the Transform class. It’s your task to script out the camera logic in the next challenge.

Since we want the camera behavior to be entirely separate from how the player moves, we’ll be controlling...

Working with the Unity physics system

Up to this point, we haven’t talked about how the Unity engine works, or how it manages to create lifelike interactions and movement in a virtual space. We’ll spend the rest of this chapter learning the basics of Unity’s physics system.

The two main components that power Unity’s NVIDIA PhysX engine are as follows:

  • Rigidbody components, which allow GameObjects to be affected by gravity and add properties such as Mass and Drag. Rigidbody components can also be affected by an applied force if they have a Collider component attached, which generates more realistic movement:
Graphical user interface, text, application  Description automatically generated

Figure 7.6: Rigidbody component in the Inspector pane

  • Collider components, which determine how and when GameObjects enter and exit each other’s physical space or simply collide and bounce away. While there should only be one Rigidbody component attached to a given GameObject, there can be several Collider...

Summary

This wraps up your first experience of creating independent gameplay behaviors and tying them all together into a cohesive, albeit simple, game prototype. You’ve used vectors and basic vector math to determine positions and angles in a 3D space, and you’re familiar with player input and the two main methods of moving and rotating GameObjects. You’ve even gone down into the bowels of the Unity physics system to get comfortable with Rigidbody physics, collisions, triggers, and event notifications. All in all, Hero Born is off to a great start.

In the next chapter, we’ll start tackling more game mechanics, including jumping, dashing, shooting projectiles, and interacting with parts of the environment. This will give you more hands-on experience in using force with Rigidbody components, gathering player input, and executing logic.

Pop quiz—player controls and physics

  1. What data type would you use to store 3D movement and rotation information?
  2. What built-in Unity component allows you to track and modify player controls?
  3. Which component adds real-world physics to a GameObject?
  4. What method does Unity suggest using to execute physics-related code on GameObjects?

Don’t forget to check your answers against mine in the Pop Quiz Answers appendix!

Join us on discord!

Read this book alongside other users, Unity game development experts and the author himself.

Ask questions, provide solutions to other readers, chat with the author via. Ask Me Anything sessions and much more.

Scan the QR code or visit the link to join the community.

https://packt.link/csharpwithunity

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning C# by Developing Games with Unity - Seventh Edition
Published in: Nov 2022Publisher: PacktISBN-13: 9781837636877
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 $15.99/month. Cancel anytime

Author (1)

author image
Harrison Ferrone

Harrison Ferrone is an instructional content creator for LinkedIn Learning and Pluralsight, tech editor for the Ray Wenderlich website, and used to write technical documentation on the Mixed Reality team at Microsoft. He is a graduate of the University of Colorado at Boulder and Columbia College, Chicago. After a few years as an iOS developer at small start-ups, and one Fortune 500 company, he fell into a teaching career and never looked back.
Read more about Harrison Ferrone