Lets Build Community

Lets Build is a community of 165 amazing members

We're a place where indie game developers to connect with one another to share ideas, ask for help and teach each other.

Create new account Log in
Cover image for Cannon Toss - It's coming at me!

Cannon Toss - It's coming at me!

cpluta profile image Chris Pluta ・5 min read

Since my last post, I mentioned I was going to add a ball with some ground... I may have done a bit more than that. πŸ˜„

Let's go over how this played out.

To start out here are the goals I achieved, create an infinite scroll game, have different obstacles that effect the movement of the ball and be able to restart whenever I wanted.

First let's check out the ball. I ended up getting Aseprite finally to try out to create the ball. Since it was fairly straight forward, I ended up recreating the cannon since I didn't import things in correctly the first time, so my pixel scale is working out much better for scale.

Now that I have a ball, I made a prefab for it. It has a RigidBody 2D so I can let gravity do its thing, and now I can drop it on the screen and let it go! It also has a Circle 2D collider as well for any collision detection that I need to do.

Now that I have that built, I need to have it hit some sort of ground. I made a big solid square to represent the ground and stretched it out across the bottom of the screen.

Now we can hit the ground, it makes sense to have a little pep in the step! So, I made a bouncy material to see how Unity handles those physics. I've never done this before but it was as simple as right click and make a new physics material 2d and attaching it to my ground! I set the bounciness to .5 to see how it looks and now there is some bounce!

Ball Drop in Editor

Next, I made the cannon shoot the ball. I just did some trigonometry to figure out the angle the player aimed the barrel for the X and Y axis to determine how much of a force needs to be added to the ball to make it go.

    public void FireBall(float angle, float power)
        var angleInRadians = angle * Mathf.Deg2Rad;
        var x = Mathf.Cos(angleInRadians);
        var y = Mathf.Sin(angleInRadians);

        var xVelocity = power * x;
        var yVelocity = power * y;

        ballXVelocity = xVelocity;

        ballBody.velocity = new Vector2(xVelocity, yVelocity);
Enter fullscreen mode Exit fullscreen mode

Since I don't want to have the ball go forever since I am actually going to freeze the x axis. So, I decided to move all the objects from right to left instead of moving the ball. So, this way the camera can stay put, and the ball seems like it has a moving effect.

Cannon Shot

By freezing x however, the x velocity gets dropped to 0. So, I need to keep track of it myself and I apply it to a GameManager since I need to access it for multiple objects.

The next challenge with the frozen ground is that because there is no velocity, the ground collision never applies its drag to the ball. So, I needed to make my own script to apply it's drag to the ball once it hits the ground.

    private void OnCollisionEnter2D(Collision2D collision)
        if (collision.gameObject.CompareTag("Player"))
            gameManager.ballXVelocity *= (1 - collision.otherCollider.sharedMaterial.friction);
Enter fullscreen mode Exit fullscreen mode

To see all these variations, I made a Canvas with a Text UI object so I can show the speed to the ball, and also to myself to make sure it's moving along well.

Now that I have some forward momentum, I can start making the infinite scroll! I can take the velocity of x and apply it to any game object I want to move it from right to left. First, I applied it to the cannon to prove it works.

To achieve this effect, I made a generic script to move the object from right to left with the ball's velocity. So, I can apply the script to any object I want and it'll move along the ground. In this script I only changed the x axis so that if I wanted to make an object like a balloon. So I can keep the y axis where the prefab is placed and just move it along.

    void Update()
        var position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);

        position.x = position.x - (gameManager.ballXVelocity * Time.deltaTime);

        this.transform.position = position;
Enter fullscreen mode Exit fullscreen mode

Next, I created a destroy script to tell and object, once it gets beyond a certain point to destroy itself, so the game isn't creating infinite objects. πŸ˜„

    void Update()
        if (parent.transform.position.x <= outOfBounds)
Enter fullscreen mode Exit fullscreen mode

Now that I have moving objects, I can now make obstacles to add some variation to the gameplay.

Before making these obstacles, I made a Spawner, that will hold the prefabs of the obstacles to randomly generate the field from. I setup a limit to the right outside of camera view to generate each object. On each update I look at the list of prefabs and randomly choose which one to make. Once the last prefab is beyond the minimum number of spaces, I randomly choose a number of tiles out to make the next object. And repeat that forever!

Now that I have a way to create obstacles, let's see what I made already.

First, there is the net. If the ball hits this location it will come to a complete stop.

Net Catch

Second, there is the TNT. Hitting this will throw the ball in a 45-degree angle at 1.5x the power as the initial blast.


Third, I made a spring board, which launches the ball at a 15-degree angle, so it moves faster forward than high.

Spring board Hit

Lastly, I made a spring which shoots at a 75-degree angle, so it's high up but not far forward.

Spring Hit

With each of these, I didn't make a stacking force yet. I just changed the current velocity to apply these new forces.

When testing this out originally, I had the ball moving too fast that it flew past obstacles and they wouldn't collide, or it went through the ground that it never bounced back.

When each object is created I place it under a SpawnedItems object so I have a single place to cleanup any previous obstacles on a restart.

Restart was fairly straight forward. I needed to make the cannon a prefab so I can recreate it later. I wired up a new input for restart, and once it was performed I destroyed the ball, cleared all obstacles, and replace the cannon in its original spot to start over again.

Alright so here is where it's at so far after all that work!

Gameplay so far

I know it was quite a busy update, but what's next?

  • Sound Effects.
  • Applying forces on obstacles and clamping them so it doesn't get too fast and collisions don't happen.
  • Coming up with a theme! Plain white objects are fun for testing it out, but I feel like it can be something more.

Is there anything you want me to dive more into? How do you feel it's shaping up? What kind of obstacles would you like to see?

Thank you for reading!


Editor guide
rcarlson profile image
Robert Carlson

What are you thinking as far as theme? We should do some brainstorming here :)

cpluta profile image
Chris Pluta Author

My default will almost always be food. πŸ˜„

Maybe launch a bagel and get stuck in honey or something. It can get some boost by jumping beans, pretzel catapult and some kind of explosive cheese?

What do you think?