• Home  / 
  • Unity3D
  •  /  Unity3D cannon / projectile ballistics

Unity3D cannon / projectile ballistics

One of the first things I created in Unity was a mobile game using cannons.  Figuring out how to launch cannonballs when I was brand new to Unity took me about a day.  But it was a fun rewarding experience, and thinking back on that experience made me want to share the process.  In this post, I’ll show how to use some commonly shared ballistics methods in a sample cannonball launching scene.

Credit

Before going too far, I want to give credit to a post that helped me when I was starting out.  This answer helped me learn and understand ballistics calculations and is the basis for this example.  http://answers.unity3d.com/comments/236712/view.html

Scene Setup

For this example, we need three things.

  • Plane
  • Cannon
  • Cannonball

Create a Plane, reset the transform values to default.

The Cannon

Create a cube.

Name it “Cannon”

Set the transform values to match these.

Disable the Box Collider

Create a material named “Cannon”, tint it grey, and assign it to the Cannon object in the scene.

The Ball

Create a sphere.

Name it “CannonBall”

Add a RigidBody component

Create a material named “CannonBall”, make it solid black, and assign it to the CannonBall object in the scene.

Copy the Transform values below onto the sphere.

The Script

Now that we have the objects setup, let’s take a look at the script.

Code Review

We start the script off with 2 private (but serialized) fields.

The first is a reference to our cannonball’s rigidbody.  To keep this simple, we’re just going to re-use the single cannonball, though in your project, you may want to reference a prefab and instantiate a new one every time the player fires instead..

Next, we have an angle variable.  Being able to adjust the angle of our cannon seems pretty important, so we have it setup as a slider you can play with in the editor.

The Update method waits for your player to click the left mouse button, then it performs a raycast, looking for the point on the plane that they’ve clicked.

If it finds a point, we call FireCannonAtPoint and send it the destination for our cannonball.

From here, we calculate the required velocity by passing our destination and angle into the BallisticVelocity() method.

We then reset the cannonball’s position to the cannon’s center (this should really be another transform at the tip of a real cannon).

And finally, we set the velocity of the cannonball’s rigidbody to the calculated velocity.

Conclusions

Shooting cannonballs, grenades, or other ‘lobbed’ projectiles is a pretty common task in games.  And as you can see above, it’s pretty easy to get working once you have a method for calculating the correct velocity.  This example even allows you to customize the angle, which makes it superior to most of the ones you’ll find in a quick search.  And again, I want to give real credit to the original answer, which I used my first time solving this problem… without people like him answering questions, I’d have blown plenty of time figuring out a workable solution 🙂