Game Development 1

3.2.1 Projectiles

In this and the next section, Students learn how to implement physically simulated cannonball projectiles as a part of the Castle Attack project.

Slides.pdf

Slides.pptx

Objectives

In this section, our goal is to create a physically simulated cannon ball projectile. To accomplish this we’re going to write a new C# script called Projectile.

New Project Setup

Create a new project using the 3D core template. You can name this project whatever you like, I named my version of the project Castle Attack but you’re welcome to change it if you’ve got a better name in mind.

Sample Scene

We’ll start by setting up our default sample scene with the following GameObjects:

  • A Plane for the ground.
  • A Sphere resembling a cannon ball.
  • An emopty GameObject called Castle with a few gray Cubes as child GameObjects.

Projectile Script

The purpose of our Projectile script is to launch a GameObject forward like a cannon ball. If you’re up for a challenge and are reading this before watching the video, you can try and write this script on your own following these guidelines:

  • The Projectile script should launch a GameObject forward when that GameObject first becomes active or enabled.
  • The Projectile motion should be physically simulated so we’re going to want to use the AddForce method on an attached Rigidbody.
  • We want to know when our projectile has hit something so we want to use a collision detection message. We haven’t covered this yet but you’re more than welcome to see if you can find some information about this in the Unity Scripting API.

Unassigned Reference Exception

Whenever we’re working with different component references in our scripts, there’s often a possibility that one or more of these references will be unassigned. If we try to access any data members from an unassigned reference, we’ll get an Unassigned Reference Exception error, which will either stop our game from running or disable the script that caused the error.

Attributes

Attributes

In Unity C# scripting, Attributes are markers that can be placed above a class, property, or function to indicate special behavior. Attributes are denoted by the [] square brackets.

Require Component

[RequireComponent] is a type of attribute that allows us to specify that a script requires a certain type of component in order to run. When we add a script with this attribute to a GameObject, if a component of that type does not exist, one will be added automatically.

In the example below, the Projectile script would require any GameObject it’s attached to, to have a Rigidbody component as well.

Code snippet image from course

GetComponent

GetComponent

In order to avoid having to set each reference variable ourselves in the editor we can instead use GetComponent to return a reference to a component of type attached to the same GameObject as the script.

Code snippet image from course

If there is not a component of the chosen type attached to that GameObject, then GetComponent will return a null reference. However, if our script is also using [RequireComponent] we can be sure that GetComponent will return a valid reference.

Castle Attack

Students become familiar with the Unity Scripting API to create a 3D castle attack arcade game reminiscent of Angry Birds.

Play in Browser

Contact us to get the full experience and try out our LMS with Self Grading Quizzes, Gradebook, Attendance Tracking, Student Reporting, Discussion Board Features and more!

Contact Us

Contact us to obtain full access!

Including...

Self-Grading Assessments

Project-Based Learning

Attendance Tracking

Student Data & Reporting

Professional Development