Going Beyond the Cardinal Point: Let’s Rotate This Bad Boy Around!

Objective: translate and rotate your Player Game Object in any direction.

In a typical 2D-style shooter game, we tend to expect our Player to move in cardinal directions, for example, UP/DOWN/LEFT/RIGHT. We may clamp the boundaries, or allow the Player Game object to “wrap” as it translates off the edge of the screen.

You can see below the typical code snippet which allows movement along the X/Y-axis, and the expected result as depicted in the GIF.

The first time I implemented this, I thought it was cool. But now, as I begin rebuilding my project from scratch, I find it somewhat limiting. I mean, what if Enemy ships come flying at me from different directions? What if I miss my shot, or find myself unable to get into position to fire before the Enemy ship passes me in the traditional top-down motion? WHAT IF????

Yes…it’s time to go down a rabbit hole…

In the new Universe I’m creating, our Player needs to rotate to face incoming threats. Let’s say we begin with a baseline of 15 degrees/second (which will be hardcoded), and multiply this by a rotation speed variable. For this, I used a private float, called it _playerRotateSpeed, and set it to a default value of 20.0f.

While looking for keyboard inputs (q, e), I then declared a new Vector3 variable for left and right rotations, playerRotateLeft, and playerRotateRight, setting them respectively to (0, 0, 15.0f) and (0, 0, -15.0f). We then need to update the rotation of the Player by increasing/decreasing its Z-axis value using transform.Rotate. In the code example below, we find ourselves rotating the Player +/- 300 degrees/second. This allows us to respond effectively to incoming threats.

There you go. We’ve met our objective. We are now able to rotate the Player while translating all over the screen. Thanks for reading :)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Michel Besnard

Michel Besnard

Military member with 35+ years of service, undertaking an apprenticeship with GameDevHQ with the objective of developing solid software engineering skills.