2 Building a demo that puts you in 3D space

 

This chapter covers

  • Understanding 3D coordinate space
  • Putting a player in a scene
  • Writing a script that moves objects
  • Implementing FPS controls

Chapter 1 concluded with the traditional “Hello World!” introduction to a new programming tool; now it’s time to dive into a nontrivial Unity project, a project with interactivity and graphics. You’ll put some objects into a scene and write code to enable a player to walk around that scene. Basically, it’ll be Doom without the monsters (something like what figure 2.1 depicts). The visual editor in Unity enables new users to start assembling a 3D prototype right away, without needing to write a lot of boilerplate code first (for things like initializing a 3D view or establishing a rendering loop).

It’s tempting to immediately start building the scene in Unity, especially with such a simple (in concept!) project. But it’s always a good idea to pause at the beginning and plan out what you’re going to do, and this is especially important right now because you’re new to the process.

Figure 2.1 Screenshot of the 3D demo (basically, Doom without the monsters)

c02-1.png
NOTE

Remember, the project for this (and all chapters) can be downloaded from the book’s website. Open the project in Unity, then open Scene to run it. While you’re learning, I recommend you type out all the code yourself and only use the downloaded sample for reference. The website’s address is www.manning.com/books/unity-in-action-second-edition.

2.1 Before you start …

2.1.1 Planning the project

2.1.2 Understanding 3D coordinate space

2.2 Begin the project: place objects in the scene

2.2.1 The scenery: floor, outer walls, and inner walls

2.2.2 Lights and cameras

2.2.3 The player’s collider and viewpoint

2.3 Making things move: a script that applies transforms

2.3.1 Visualizing how movement is programmed

2.3.2 Writing code to implement the diagram

2.3.3 Local vs. global coordinate space

2.4 Script component for looking around: MouseLook

2.4.1 Horizontal rotation that tracks mouse movement

2.4.2 Vertical rotation with limits

2.4.3 Horizontal and vertical rotation at the same time

2.5 Keyboard input component: first-person controls

2.5.1 Responding to keypresses

2.5.2 Setting a rate of movement independent of the computer’s speed

2.5.3 Moving the CharacterController for collision detection

2.5.4 Adjusting components for walking instead of flying

Summary