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

Michel Besnard
3 min readAug 23, 2022

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.

PlayerMovement() function, updated every single frame, looking for keyboard input and then translating the Player as directed. The second part of the code checks to see if the Player has reached set boundaries, and if yes, it either clamps the Player (Y-axis) or moves them to the opposite side of the Game Scene (X-axis).
Resulting Player movement in the Game Scene.

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.

Adding this code to the PlayerMovement() function will designate the “q” and “e” keys to control the CW and CCW rotation of the Player Game Object.
Exposed flanks? No way, Man! Bring it on!…

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 :)

--

--

Michel Besnard

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