I’m looking for feed back on the logic behind my change to the code. On this small scale it doesn’t really matter, but I’m trying to build good habits for when I get to larger code.
Here is my reasoning behind my change. Since the left and right rotation are the same other than the direction (+/-), I created a “direction” variable. Then I pulled the actual rotation commands out of the IF statement and pasted it below the IF.
Pros:
Allows to change one calculation if it was necessary later. (Less code maintenance)
Cleaner looking code (Easier Code maintenance)
Cons:
You are technically always rotating, and calculating a rotation every frame. (More CPU usage)
Another variable (More RAM usage)
Any comments or thoughts on this matter are appreciated.
Thanks
My Rotate Code
void Rotate()
{
rigidBody.freezeRotation = true;
float rotationThisFrame = rotateThrust * Time.deltaTime;
int direction = 0;
if (Input.GetKey(KeyCode.A)) { direction = 1; }
else if (Input.GetKey(KeyCode.D)) { direction = -1; }
transform.Rotate(direction * Vector3.forward * rotationThisFrame);
rigidBody.freezeRotation = false;
} // My Rotate Method
Here is the Standard Rotate method for comparison
void StandardRotate()
{
rigidBody.freezeRotation = true;
float rotationThisFrameStandard = rotateThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward * rotationThisFrameStandard);
print("A Key Pressed - Rotate Left");
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward * rotationThisFrameStandard);
print("D Key Pressed - Rotate Right");
}
rigidBody.freezeRotation = false;
} //Standard Rotate Method