Adding a top-down strafe feature

Yesterday I was tasked to add a strafe feature in our current project, Bounce Ball. Bounce Ball, or “BB” for short, is a top-down 3D game with free rotation of the character. The team have kept an ongoing discussion of how the strafe should be implemented. We all agree on adding what we call rotational locks that snap the player to an appropriate angle but the question has been: how many rotations do we add?
Eight rotations feels like a minimum requirement but sixteen feels like to many! I’ve visualized these rotations in the picture below.
The left image shows the eight rotations while the right image shows the sixteen rotations. We feel like eight rotations will snap the character too much and affect game feel, while sixteen rotations will require too much precision. We decided to make it twelve rotations and play test it to get a feeling for it.
I started by adding a public array of integer values clamped by the range of 0-359, in the PlayerMovement class. This array is meant to store the specific angles that represents the rotational locks to snap to.
Now I had to create a method that runs once, each time the strafe button gets pressed (we use the left trigger), and snaps the character to the closest rotational lock. To do this I will need to iterate through the array and compare each value with the characters current rotational angle.
McClusky idle.
Stan McClusky – Made by Marcel Fontes @Marcel_E_Fontes.
This is how I wrote that method:

private void LockRotation() {
    // Get the current rotation of the characters Y-axis,
    // since that's the only relevant one.
    float currentRotation = p_Transform.eulerAngles.y;

    // Save the locked rotation closest to current.
    float closestRotation = 0f;

    // Start with the highest possible angle.
    float smallestDifference = 360f;

    // Iterate through the rotational locks-array.
    for(int i = 0; i < p_RotationLocks.Length; i++) {
        // Check the difference between this rotation lock
        // and the current rotation.
        float t = Mathf.Max(p_RotationLocks[i], currentRotation)
                - Mathf.Min(p_RotationLocks[i], currentRotation);

        // If the difference is smaller than the previous...
        if(t < smallestDifference) {
            smallestDifference = t; // set it as the smallest...
            // and save the rotation lock.
            closestRotation = p_RotationLocks[i];
        }
    }

    // Calculate the difference to adjust in the current rotation.
    float difference = closestRotation - currentRotation;

    // Rotate the player.
    p_Transform.eulerAngles = p_Transform.eulerAngles
                            + new Vector3(0f, difference, 0f);
}

If you have any thoughts, comments or questions don’t be afraid to post them below!

Comments