Unity Lab #5: Raycasting

When you set up a scene in Unity, you’re creating a virtual 3D world for the characters in your game to move around in. But in most games, most things in the game aren’t directly controlled by the player. So how do these objects find their way around a scene?

The goal of labs 5 and 6 is to get you familiar with Unity’s pathfinding and navigation system, a sophisticated AI system that lets you create characters that can find their way around the worlds that you create. In this lab, you’ll build a scene out of GameObjects and use navigation to move a character around it.

You’ll use raycasting to write code that’s responsive to the geometry of the scene, capture input, and use it to move a GameObject to the point where the player clicked. Just as importantly, you’ll get practice writing C# code with classes, fields, references, and other topics we’ve discussed.

Create a new Unity project and start to set up the scene

Before you begin, close any Unity project that you have open. Also close Visual Studio—we’ll let Unity open it for us. Create a new Unity project using the 3D template, set your layout to Wide so it matches our screenshots, and give it a name like Unity Labs 5 and 6 so you can come back to it later.

Start by creating a play area that the player will navigate around. Right-click inside the Hierarchy window and create a Plane (GameObject >>3D Object >> Plane). Name your new Plane GameObject Floor.

Right-click on the Assets folder in the Project ...

Get Head First C#, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.